471,337 Members | 907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Suspended Thread

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
Jul 21 '05 #1
4 2333
>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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2

"Guido Kraus" <gu*********@newsgroup.nospam> wrote in message
news:06**********************************@microsof t.com...
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.
Jul 21 '05 #3
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

"Willy Denoyette [MVP]" wrote:

"Guido Kraus" <gu*********@newsgroup.nospam> wrote in message
news:06**********************************@microsof t.com...
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.

Jul 21 '05 #4
Guido Kraus <gu*********@newsgroup.nospam> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Carlos Kirkconnell | last post: by
1 post views Thread by Andreas Müller | last post: by
1 post views Thread by Brett | last post: by
2 posts views Thread by Mark Denardo | last post: by
4 posts views Thread by Guido Kraus | last post: by
1 post views Thread by Christopher Stott | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.