in Education by
I want to extend the TextBox control, so it will fire a custom event with a specified delay. Here is the code i have so far: public class DelayTextBox : TextBox { private Timer _delayTimer; private int _threshold = 1000; public DelayTextBox() { _delayTimer = new Timer(_threshold); _delayTimer.Elapsed += new ElapsedEventHandler(_delayTimer_Elapsed); } public int Delay { set { _threshold = value; } } private void _delayTimer_Elapsed(object sender, ElapsedEventArgs e) { _delayTimer.Stop(); RaiseDelayedTextChangedEvent(); } protected override void OnTextChanged(TextChangedEventArgs e) { _delayTimer.Stop(); _delayTimer.Start(); } private static readonly RoutedEvent DelayedTextChangedEvent = EventManager.RegisterRoutedEvent( "DelayedTextChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(DelayTextBox)); public event RoutedEventHandler DelayedTextChanged { add { AddHandler(DelayedTextChangedEvent, value); } remove { RemoveHandler(DelayedTextChangedEvent, value); } } private void RaiseDelayedTextChangedEvent() { RoutedEventArgs newEventArgs = new RoutedEventArgs(DelayTextBox.DelayedTextChangedEvent); RaiseEvent(newEventArgs); } } The problem is that whenever i fire RaiseDelayedTextChangedEvent(), i get an exception, telling me 'The calling thread cannot access this object because a different thread owns it.' The exception is thrown here: private void RaiseDelayedTextChangedEvent() { RoutedEventArgs newEventArgs = new RoutedEventArgs(DelayTextBox.DelayedTextChangedEvent); RaiseEvent(newEventArgs); } <---- Here JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
That's because the Timer runs in a different thread, and UI cannot be modified from that thread (like your exception says). Try using a DispatcherTimer instead. You can learn more about it here: http://code.dortikum.net/2008/08/06/timer-vs-dispatchertimer-in-wpf/

Related questions

0 votes
    I want to extend the TextBox control, so it will fire a custom event with a specified ... new RoutedEventArgs(DelayTextBox.DelayedTextChangedEvent); RaiseEvent(newEventArgs); }...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I have a Theme attribute that I can load like this: AeroTheme is a class. How can I achieve the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I have a Theme attribute that I can load like this: AeroTheme is a class. How can I achieve the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
0 votes
    I'm trying to get a fresh little reactUI WPF app up and running. However ViewModelViewHost causes me ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    how would i add multiple buttons to a window in c#? here's what i need to do... i'm getting ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    When dealing with mobile clients it is very common to have multisecond delays during the transmission of HTTP ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I have a PHP blade file and I have a list of components working as they but can't seem to pass ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am building a basic RSS reader app that, which the 'story' is clicked, the information for it is displayed in a modal ... .log('Does Not Work!'); Or another way: - overscroll...
asked Jul 30, 2022 in Education by JackTerrance
0 votes
    I'm working on a program that displays a list from an SQFlite table. The future for the snapshot ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    A rifleman is firing at a distance target and hence has only 10% chance of hitting it. Find the number of rounds; he ... C. 7 D. 5 Select the correct answer from above options...
asked Nov 16, 2021 in Education by JackTerrance
0 votes
    What are advantages of unit (or) direct system of pulverised fuel firing?...
asked Feb 3, 2021 in Technology by JackTerrance
0 votes
    Name the two methods of pulverised fuel firing system?...
asked Feb 3, 2021 in General by JackTerrance
0 votes
    What is the difference between stocker firing and pulverised fuel firing?...
asked Feb 2, 2021 in General by JackTerrance
0 votes
    how to pass a value to a react funtion without immediately firing it?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    What is custom slide show? Please answer the above question....
asked Aug 23, 2022 in Education by JackTerrance
...