thread hang when starting from static class (not a form)

  • Thread starter Thread starter Serge
  • Start date Start date
S

Serge

Hi,

I am having a thread hang problem in my c# code.

The example on the website: http://csharp.web1000.com/ is a
simplified version of my problem.

You will see in the form that a method TestThread increments a number in the
textbox on the form.
TestThread is called from a worker thread (2nd thread) using a TimerThread.
Every 2 seconds it will increment the number in te text box.

First start the project from the form (form1 class is the startup object).
The Form is displayed. Now push the button, this will create the
TimerThread.
You now see nicely that the number increments every 2 seconds.

Now start the application from the class (class1 is the startup object).
Now the TimerThread will be created in the Class1 object.
You will see that the number is never incremented.

When stepping through the code you will see that the TestThread method is
called.
The TestThread code in the form properly detects that this is run from a
second thread (line InvokeRequired) and continues to the line where Invoke
is called. I use Invole so that the increment function would run on the
forms thread. It is here where the application seems to hang. The number in
the textbox is never incremented.

Can anyone explain me why this is happening or how to solve this ?
i was wondering, might this have something to do with Apparementthreading ?
(this is used when you start the application from the form (=startup
object)).
Maybe when you start it from the class this is no longer the case ?
Maybe it's something completely different.
Thanks for shedding some light in the darkness.

Regards, Serge



http://csharp.web1000.com/
 
You need to start the windows message pump... the way you do it in .Net is
by calling Application.Run(...). When you set your startup object to the
class that is not a winform, you never di this. That's why the thread hangs.

There are a lot of discussions in google groups that talk about this, and
I'm sure MSDN has something on the subject as well. Maybe someone else can
provide some links. Here is one (watch for line wrap):

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=u3suQAxXAHA
..236%40cppssbbsa02.microsoft.com

Hope that helps,
-JG
 
yes it helped a lot.

I now have my application working with the message pump running (using the
Application.Run)
Thanks VERY much !!!
 
Back
Top