Cross-thread operations with PostSharp
When you try to inform User Interface (UI) about the background operation progress or completion, you can not do it from the background thread.
UI doesn’t like to be informed about anything from a different thread: you’ll get “System.InvalidOperationException: Cross-thread operation not valid: Control ‘xxx’ accessed from a thread other than the thread it was created on.” exception from WinForms control, if you try:
public void ShowStatus(ApplicationStatus status) { this._lblServiceAddress.Text = "Connected to: " + status.WebServiceAddress; this._lblUserId.Text = "Working as: " + status.UserId; }
The easiest sollution is to use BeginInvoke method on the control Control or Form:
public void ShowStatus(ApplicationStatus status) { this.BeginInvoke((MethodInvoker)(() => { this._lblServiceAddress.Text = "Connected to: " + status.WebServiceAddress; this._lblUserId.Text = "Working as: " + status.UserId; })); }
Well, it’s fun to write this once, but if you have many operations done in background sooner or later you’d like to have something nicer. Like an attribute for example:
[ThreadAccessibleUI] public void ShowStatus(ApplicationStatus status) { this._lblServiceAddress.Text = "Connected to: " + status.WebServiceAddress; this._lblUserId.Text = "Working as: " + status.UserId; }
Here’s the attribute implementation of such attribute using PostSharp 1.5:
/// <summary> /// PostSharp attribute. /// Use it to mark Control's methods that /// are invoked from background thread. /// </summary> /// <remarks> /// Be careful as BeginInvoke uses the message queue. /// This means that the interface will be refreshed /// when application has a chance to process its messages. /// </remarks> [AttributeUsage(AttributeTargets.Method)] [Serializable] // required by PostSharp public class ThreadAccessibleUIAttribute : OnMethodInvocationAspect { public override void OnInvocation( MethodInvocationEventArgs eventArgs) { Control control = eventArgs.Instance as Control; if (control == null) throw new ApplicationException( "ThreadAccessibleUIAttribute" + "can be applied only to methods on Control class"); // The form may be closed before // this method is called from another thread. if (control.Created == false) return; control.BeginInvoke((MethodInvoker)eventArgs.Proceed); } };