I am trying to use threadsafe calls to UI controls from a C# form. For some reason the BackgroundWorkerHelper is not available, might be because of the version of C# or becuase I am deploying to a “Window CE 5.0 Device”.
I have this
//For ThreadSafe called to edit components
private delegate void SetControlPropertyThreadSafeDelegate(Control control, string propertyName, object propertyValue);
public static void SetControlPropertyThreadSafe(Control control, string propertyName, object propertyValue)
{
if (control.InvokeRequired)
{
control.Invoke(new SetControlPropertyThreadSafeDelegate(SetControlPropertyThreadSafe), new object[] { control, propertyName, propertyValue });
}
else
{
control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
}
}
And am calling the Thread like this
private Thread workerThread = new Thread(new ThreadStart(this.remoteRequestBackgroundTask));
workerThread.Start();
Then inside the remoteRequestBackgroundTask() function I change controls like this
//enable the cancel button
SetControlPropertyThreadSafe(btnCancel, "Enabled", true);
The problem is that when it runs, debugging stops here
control.GetType().InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { propertyValue });
and I get “NotSupportedException”
Have a look at the MSDN Documentation for .InvokeMember, under Exceptions. Next to
NotSupportedException, you’ll see:In that
elseblock, I believe you could do something like this instead. This is shortened from some very similar .NET Compact Framework code I have: