Guide on Cross Threading  
Author Message
FlyinBrian





PostPosted: Visual C# General, Guide on Cross Threading Top

Anyone have a good guide on cross threading Essentially, I'm trying to access a Windows form control outside of the thread it was created in. Any help would be greatly appreciated.


Visual C#20  
 
 
ahmedilyas





PostPosted: Visual C# General, Guide on Cross Threading Top

you would need to invoke that UI control from the other thread.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=564275&SiteID=1

and no, don't set the CheckForIllegalCrossThreads to false.



 
 
FlyinBrian





PostPosted: Visual C# General, Guide on Cross Threading Top

im not sure I follow. I basically want to call user.text.enabled = false within a thread but im getting the cross thread error.

 
 
ahmedilyas





PostPosted: Visual C# General, Guide on Cross Threading Top

:-) have you followed the example in the link supplied

in order to access a UI control from another thread, you need to invoke that control, an example on how to do this is given in the link

so in your case you may wish to do this.

//main form

public delegate void MyTextBoxDelegate(bool enable);

..

..

public void DoEnableTextBox(bool enable)

{

   if (this.theTextBox.InvokeRequired)

   {

      MyTextBoxDelegate theDelegate = new MyTextBoxDelegate(this.DoEnableTextBox);

      this.Invoke(theDelegate, new object[] { enable });

   }

   else

   {

      this.theTextBox.Enabled = enable;

   }

}

 

usage:

call from the other thread:

DoEnableTextBox(true); //true or false, whichever you want to use



 
 
FlyinBrian





PostPosted: Visual C# General, Guide on Cross Threading Top

thanks, can you please briefly explain the theroy behind this invokerequired I would really appreciate it.

 
 
ahmedilyas





PostPosted: Visual C# General, Guide on Cross Threading Top

no worries.

Basically you need to invoke the UI control so it can then do the operation you requested, its the correct way of handling UI controls when accessing from one thread to another. The InvokeRequired is just a bool value which indicates if the control/UI control requires to be invoked in order to access it, if so, it will then call the method again from the "owner" of that control and do whatever it is that needs to be done (in the else statement)

http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx



 
 
FlyinBrian





PostPosted: Visual C# General, Guide on Cross Threading Top

Controls work fine, now how bout hiding the Form itself and calling another

 
 
ahmedilyas





PostPosted: Visual C# General, Guide on Cross Threading Top

in what way I guess you would do the same thing if you want your main thread (UI) to call/create/show another form rather than from the other (non UI) thread.

the code posted/link posted applies generally for anything you do cross threading, however the only thing that changes is just the parameters really and thats it. So instead of taking in a bool value, you can have an int value, or a string, or whatever. The delegate signature must match the method signature, as you can see