R
robert.waters
I have a helper class that I instantiate from my UI that creates a
worker thread, processes something, and provides an event handler to
tell the caller that it has some output from that process; I would
like to update a textbox with that output.
In order to do this, however, I need to use the TextBox.Invoke()
method, because the event handler is running on another thread. But,
this creates a lot of code that looks like it should be unnecessary: a
delegate that is *only* used as as parameter to Control.Invoke(), and
the function that is passed to Invoke().
This is the code:
-------------------------------------
Form_Load()
{
Processor p = new Processor(some args);
p.OutputRecd += new
ProcessorOutputEventHandler(this.UpdateTextbox)
p.Start();
}
private delegate void DelegateRequiredForInvoke(string text);
private void UpdateTextBox(object sender, ProcessorOutputEventArgs e)
{
TextBox1.Invoke(new DelegateRequiredForInvoke(WriteToTextBox),
e.Output);
}
private void WriteToTextbox(string text)
{
TextBox1.AppendText(text);
}
-------------------------------------
Why must the function WriteToTextBox() even exist? In a non-threaded
world, I could update the textbox in the UpdateTextBox() function.
Because of threading, not only do I need an additional function, but I
need a delegate that points to that function.
It seems like, for lack of a better word, 'cruft'.
Thank you for your help,
Robert Waters
worker thread, processes something, and provides an event handler to
tell the caller that it has some output from that process; I would
like to update a textbox with that output.
In order to do this, however, I need to use the TextBox.Invoke()
method, because the event handler is running on another thread. But,
this creates a lot of code that looks like it should be unnecessary: a
delegate that is *only* used as as parameter to Control.Invoke(), and
the function that is passed to Invoke().
This is the code:
-------------------------------------
Form_Load()
{
Processor p = new Processor(some args);
p.OutputRecd += new
ProcessorOutputEventHandler(this.UpdateTextbox)
p.Start();
}
private delegate void DelegateRequiredForInvoke(string text);
private void UpdateTextBox(object sender, ProcessorOutputEventArgs e)
{
TextBox1.Invoke(new DelegateRequiredForInvoke(WriteToTextBox),
e.Output);
}
private void WriteToTextbox(string text)
{
TextBox1.AppendText(text);
}
-------------------------------------
Why must the function WriteToTextBox() even exist? In a non-threaded
world, I could update the textbox in the UpdateTextBox() function.
Because of threading, not only do I need an additional function, but I
need a delegate that points to that function.
It seems like, for lack of a better word, 'cruft'.
Thank you for your help,
Robert Waters