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)