Marc Bartsch wrote:
Quote:
I am not sure whether you can raise the event on the UI thread without a
reference to the UI, but you can do the following in your form that
processes the EngineMessage event without explicitly defining so many
delegates:
>
class MyForm : Form
{
// Process EngineMessage here
protected void Engine_EngineMessage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((MethodInvoker)delegate { SetText(msg); });
}
Or, if it's really a simple method like that (it's not always, but in
this example obviously it is :) ):
// Process EngineMessage here
protected void Engine_EngineMessage(string msg)
{
// Invoke SetText on UI thread using an anonymous method and
// cast it to MethodInvoker.
this.Invoke((MethodInvoker)delegate { this.myTextBox.Text = msg });
}
Actually, for that matter, unless I needed to use the same code
somewhere else by calling it as a method, I would probably just put the
whole invoke-necessary code in the anonymous delegate. It's just that
as the anonymous delegate gets longer, the odds that it's doing
something that needs to be shared by non-invoking code goes up, thus
justifying putting it into a shareable method. :)
Pete