Events && Multi-Threading
January 22, 2013 Leave a comment
Dear Future Me:
When you need to write a vanilla event, use the following boilerplate:
public event EventHandler<EventArgs> MyEventName
And when you need to write a vanilla cross-thread function to update the UI in a WPF application, use the following boilerplate:
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { SomeUIControl.Text = someValueFromThisThread; }));
And this is in System.Windows.Threading, which is not in the Client Profile
And when you need to write a vanilla cross-thread function to update the UI in a WinForms application, use the following boiler plate:
this.BeginInvoke(new Action(() => { StringBuilder message = new StringBuilder(); message.AppendFormat("{0:hh\\:mm\\:ss}", e.ElaspedTime); SomeUIControl.Text += Environment.NewLine; SomeUIControl.Text = message.ToString(); }));
Love,
Present Me
PS: you really should exercise more…