Suspended Thread

G

Guest

Hi,

I have a VB Windows Forms app that has a single form ('MainForm'). MainForm
has a shared (C#: static) class variable that holds a reference to a newly
created Thread. This thread does some work and then supends itself
(Thread.CurrentThread.Suspend()). When the user closes the MainForm window
the application does not quit since it has one thread that is not finished
(the one which is suspended).

What is the suggested way of quitting an application that might have
suspended and/or waiting (sleeping) threads?

Thanks for any ideas,
Guido
 
M

Mattias Sjögren

What is the suggested way of quitting an application that might have
suspended and/or waiting (sleeping) threads?

If you set the thread's IsBackground property to True it will not
prevent the application from terminating.

That said, why do you suspend the thread rather than let it finish? If
it has to be suspended, can't you set a flag at shutdown to indicate
that the thread should finish and then wake it up?



Mattias
 
W

Willy Denoyette [MVP]

Guido Kraus said:
Hi,

I have a VB Windows Forms app that has a single form ('MainForm').
MainForm
has a shared (C#: static) class variable that holds a reference to a newly
created Thread. This thread does some work and then supends itself
(Thread.CurrentThread.Suspend()). When the user closes the MainForm window
the application does not quit since it has one thread that is not finished
(the one which is suspended).

What is the suggested way of quitting an application that might have
suspended and/or waiting (sleeping) threads?

Thanks for any ideas,
Guido

Don't suspend a thread use a wait primitive like an event instead.
BTW. Thread.Suspend will go away in v2.0.

Willy.
 
G

Guest

Thanks for your ideas.
I changed my code from Thread.CurrentThread.Suspend() to
Thread.Sleep(Timeout.Infinite)
To let it awake I use myThread.Interrupt().

Unfortunately I cannot let the thread finish and use a new one. The problem
has to do with unmanaged code. To terminate my thread I subscribe to the
System.Windows.Forms.Application.ApplicationExit event from within my thread
so that it can Abort() itself.

As far as I can see this solution works for me but I will look at the
IsBackground property to see if this is a more elegant way.

Thanks,
Guido
 
J

Jon Skeet [C# MVP]

Guido Kraus said:
Thanks for your ideas.
I changed my code from Thread.CurrentThread.Suspend() to
Thread.Sleep(Timeout.Infinite)
To let it awake I use myThread.Interrupt().

That's not a terribly nice idea. Use Monitor.Wait/Pulse or a
Manual/AutoResetEvent - interrupting a thread just to wake it up is
like detecting the end of a loop which iterates through an array by
letting it go off the end of the array and catching the out of bounds
exception.
Unfortunately I cannot let the thread finish and use a new one. The problem
has to do with unmanaged code. To terminate my thread I subscribe to the
System.Windows.Forms.Application.ApplicationExit event from within my thread
so that it can Abort() itself.

As far as I can see this solution works for me but I will look at the
IsBackground property to see if this is a more elegant way.

That would certainly terminate the process without you having to
terminate the thread, if that's all you need to do.
 

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

Top