Multithreading and applicantion hangs




I am developing a Multithreaded Server based Task Scheduler wich runs
on the server and informs all the connected client when a particular
schedule occurs to do the assigned task.

Following is what I am doing to give you all an idea about the
application and how I have done it

I have a windows based application. This is a client which consists of
2 Forms viz Form1 and Form2 where Form1 is the main form and Form2 is
the form called by the application when the schdule occrs

I have Server (a dll) which has a main class called Class1 and another
class called Class2 which contains the information of each schedule.
The object of Class2 is started on each seperate thread by main the
object of main class as a worker thread. The Class2 has the
System.Timers.Timer (a server based timer) which got started when the
thread is started.

Now on each worker thread, when timer elasped it informs the main
class1 via delegate that a schedule has occured. The main class then
informs all the connected client that a schedule has occured. The the
Form1 gets this information it opens the Form2 and here is the problem.

When form1 tries to open the form2, the form2 hangs and doesn't show at
all. In the Task Manager it shows that the application is not
responding and then I have to kill the application.

Can anybody point out what could be going wrog or what is the wrong
thing which I am doing because of which such behaviour is shown.


Prateek Baxi


You may not access GUI components (including your form) from a worker
thread. You need to use Control.Invoke to marshal the call into the
correct thread.


Stoitcho Goutsev \(100\)

Don't ever call control methods and set properties from a thread different
than the one created the control. Of course there some that are safe to be
called, but as a rule of thumb don't do that. When you want to access
control form worker thread use Control.Invoke to marshal the code. In order
to find out where is ssafe to call a method or set a property you can use
Control's IsInvoleRequired property.

For more info:

Brian Gideon


In addition to what others have said I usually prefer
Control.BeginInvoke. That way the worker won't block waiting for the
UI to process the message. Of course, it depends on the exact behavior
I'm after.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question