I know illegal cross-thread calls in WindowsForms. I guess that you're using them.
To overcome the error, you have to refrain from using any property of any control from your worker thead. Instead, make the GUI thread do the work. An example
private void SetLabelText( string text)
{
label1.Text = text;
}
This one will result in an error when called from a thread other than the GUI thread. Put some thead-border-crossing code in the method:
private void SetLabelText( string text)
{
if( label1.InvokeRequired)
{
label1.Invoke( new Action<string>(SetLabelText, new object[]{text}));
return;
}
label1.Text = text;
}
As you can see, the immediate label-changing code stays the same. The
if
-branch makes the GUI thread execute the method again and exit before doing the actual work. In the GUI thread, the method is then executed again, not entering the
if
-branch and executing the actual label changing code.
Since the GUI thread is the one that originally created
label1
, it is allowed to use its properties (any thread is allowed to use the special property
InvokeRequired
as well as methods
Invoke
and
BeginInvoke
).