When i try to change a UI property (specifically enable) my thread throws System.Threading.ThreadAbortException
How do i access UI in a Thread.
When i try to change a UI property (specifically enable) my thread throws System.Threading.ThreadAbortException
How do i access UI in a Thread.
How about using Win Form's BackgroundWorker class instead of manual thead synchronization implemenation?
I assume we're talking WinForms here? You need to have a single thread managing this - the thread that created the control in question. If you want to do this from a different thread, which you can detect using Control.InvokeRequired, then you should use the Control.Invoke method to marshall this onto the correct thread. Google that property and method (respectively) for some common patterns in doing this.
Use SynchronizationContext
to marshal calls to the UI thread if you want to modify UI while you non-UI thread is still running. Otherwise, use BackgroundWorker
.
void button1_Click( object sender, EventArgs e ) {
var thread = new Thread( ParalelMethod );
thread.Start( "hello world" );
}
void ParalelMethod( object arg ) {
if ( this.InvokeRequired ) {
Action<object> dlg = ParalelMethod;
this.Invoke( dlg, arg );
}
else {
this.button1.Text = arg.ToString();
}
}
You could use a BackgroundWorker and then change the UI like this:
control.Invoke((MethodInvoker)delegate {
control.Enabled = true;
});
If you are using C# 3.5, it is really easy to use extension methods and lambdas to prevent updating the UI from other threads.
public static class FormExtensions
{
public static void InvokeEx<T>(this T @this, Action<T> action) where T : Form
{
if (@this.InvokeRequired)
{
@this.Invoke(action, @this);
}
else
{
action(@this);
}
}
}
So now you can use InvokeEx
on any form and be able to access any properties/fields that aren't part of Form
.
this.InvokeEx(f => f.label1.Text = "Hello");