It's easiest just to use a lambda expression:
private void doWork()
{
// Name changed to avoid it being a keyword
MyClass clazz = new MyClass();
clazz.StatusChanged += (sender, args) => {
string message = (string) sender; // This is odd
Action action = () => textBox.Text = message;
Dispatcher.Invoke(action);
};
clazz.DoStuff();
}
It's very odd using a string as the sender of an event though - it would be better to use a subclass of EventArgs
, and make that store the message - then make your event use EventHandler<T>
instead of just EventHandler
.
The above code is slightly confusing because you're subscribing to the event with one delegate, then passing another to Dispatcher.Invoke
- hence the two lambda expressions (one inside another). You could always use a method group instead, at least for the outer one:
private void doWork()
{
// Name changed to avoid it being a keyword
MyClass clazz = new MyClass();
clazz.StatusChanged += HandleStatusChange;
clazz.DoStuff();
}
private void HandleStatusChange(object sender, EventArgs e)
{
string message = (string) sender; // This is odd
Action action = () => textBox.Text = message;
Dispatcher.Invoke(action);
}