Click to See Complete Forum and Search --> : GUI Threading in C#


XxXxX
March 18th, 2003, 04:42 PM
I'm not sure whether I can describe this problem clearly, here is my best shot.

I had a class say ClassA and a method MethodA. In MethodA, it will spawn several threads to do their work. Within these spawned threads, some progress information will be reported. Now the problem is that my classes works fine in command line env (e.g. if I just print out some message to stdout from spawned threads for reporting progresses) but failed in a GUI env (e.g. reporting by adding text to a text component). Seems the problem is that my code called a gui function (like TextBox.text="xxx") in some other threads than the from the default gui thread.

I remembered reading some material on this, like the gui event handlers runs in a thread and you can't just call a gui method from other threads. You need some special treatment. But I can't remember the source. Could anyone help me?

Thank.

pareshgh
March 18th, 2003, 04:45 PM
have you tried using Timer class.
I think you should try for that.

Paresh

XxXxX
March 18th, 2003, 04:49 PM
You mean using a timer to periodically check status info for sub-threads? That would avoid calling back a gui function from non-gui threads.

I'll try that I think.

pareshgh
March 18th, 2003, 05:22 PM
Timer is a UI control when u drag and drop it in ur form and just implement the method ...
and start timer in load method of the form.

so u can there update UI.

there are some conflicts btw threads and UI updates so u need to carefully design it..
Paresh

petru66
March 19th, 2003, 07:33 AM
I think the following article gives the answer to the initial question:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

Its sequels are also worth reading - a little bit more advanced, though.

It helped me a lot.

Petru

XxXxX
March 19th, 2003, 10:46 AM
Originally posted by petru66
I think the following article gives the answer to the initial question:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms06112002.asp

Its sequels are also worth reading - a little bit more advanced, though.

It helped me a lot.

Petru

Thanks, this is just what I'm looking for.

pareshgh
March 19th, 2003, 01:48 PM
nice article though ! ;)
actually when you create a multithreading on your own then only you will realize pits-and-pots of it. :D

XxXxX
March 19th, 2003, 03:13 PM
Exactly! Once I tried to do in VB6 and resulted in a total mess. :confused:

pareshgh
March 20th, 2003, 07:43 PM
what type of mess thou.:(