[C#] Cross-Threading ?

10/11/2008 13:36 Atheuz#1
Hello,

Is there a flag or whatever that lets me use crossthreading freely? Without the help of a Workerthread. Whenever I try to change a variable it doesn't let me because Crossthreading isn't safe (eg. accessing the variables from other threads), but It's also hard to find a solution for this.
I'm starting another thread that contains a function to download the source code of another page and process the wanted information into variables, however I cannot do that with a thread. The only solution I could think of right now would be to save everything into a file and then let the program red it on the end of the batch. Any solutions?
10/11/2008 17:39 Cholik#2
Crossthreadexceptions should only be a problem when working with Forms in .NET right ?
Mh I think I remember that you had to invoke the functions of another thread before you could use them.
Although I can't help you directly with that try to find some example at [Only registered and activated users can see links. Click Here To Register...]

I'll move this to epvp*coders. They'll be able to help you with this. I guess we got a lot of C# coders in here.

#moved
10/11/2008 18:01 Bot_interesierter#3
How about using shared memory :-)?
I think this can be done in C#
10/11/2008 20:31 Atheuz#4
Quote:
Originally Posted by al_j View Post
Crossthreadexceptions should only be a problem when working with Forms in .NET right ?
Mh I think I remember that you had to invoke the functions of another thread before you could use them.
Although I can't help you directly with that try to find some example at [Only registered and activated users can see links. Click Here To Register...]

I'll move this to epvp*coders. They'll be able to help you with this. I guess we got a lot of C# coders in here.

#moved
It is, however even trying to this
string mytextbox = textBox1.Text;
and to change it lateron as mytextbox during the thread will also result in a
crossthread exception. And appartly I also cannot invoke functions of a different thread, without having to stop the current thread first.

Quote:
How about using shared memory :-)?
Explain.
10/12/2008 00:15 Bot_interesierter#5
You put the var in a shared memory section so every thread can access it, I'm not shure if it works, it's just an idea :-)
10/13/2008 16:19 mondesser#6
you cannot access any part of the gui from another thread.
use Control.Invoke if you want to change the gui from a different thread.

please add some example code, its hard to help you without any code.
10/13/2008 16:22 Atheuz#7
Quote:
Originally Posted by mondesser View Post
you cannot access any part of the gui from another thread.
use Control.Invoke if you want to change the gui from a different thread.

please add some example code, its hard to help you without any code.
Doesn't that method require a Worker Thread?
10/14/2008 08:41 mondesser#8
As far as i understood, you want a thread, that does something and then reports back to the gui.

you can:
-throw an event from your thread and catch it in the gui.
But you still have to call Invoke in the event handler.

-define a function in the gui class that does all the changes to the gui and invokes itself if an invoke is required, then call this function from the thread:
Code:
delegate void UpdateGuiDelegate();
public void UpdateGui()
{
  if (this.InvokeRequired)
  {
     UpdateGuiDelegate del = new UpdateGuiDelegate(AddressOf UpdateGui); //AddressOf is vb.net syntax, i forgot the C# syntax ^^
     this.Invoke(del);
  }
  else
  {
     //do your gui work here
  }
}
Invoke is a synchronous call, so in any case your thread will wait for the gui to finish its job. If you don't want that, you can call Control.BeginInvoke which is an asynchronous call.