472,789 Members | 856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 2396
>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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carlos Kirkconnell | last post by:
I'm programming a multi - threaded application using C#. I have two questions regarding to the use of threads 1- I have a Hashtable that will have multiple writters and multiple readers. I used...
3
by: CMan | last post by:
Hi, We are currently trying to install .Net Framework v.1.1 on a server which already has v1.0. We are receiving the following error. Error 1704.An installation for Microsoft .NET Framework...
1
by: Amratash | last post by:
I'm receiving the above mentioned error.The problem is: "I suspend one thread.Another thread is resuming the first thread.Then I added following code:Its in simple english if(first thread is not...
1
by: Andreas Müller | last post by:
Hi All, What I want to do, is to reuse a thread after it was supended. However, if the thread was suspended for a while and I reinvoke it using Thread.Resume(), an exception is thrown. If I look...
1
by: Brett | last post by:
Once a thread is put into suspended mode, how is it restart? Calling Thread.start() will throw an error. Thanks, Brett
2
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional...
4
by: Guido Kraus | last post by:
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...
1
by: Christopher Stott | last post by:
I'm working on a PIM program, which can function as an alarm clock. It needs to be able to suspend the computer, and wake it up at a particular time. As a test, I'm using a Waitable timer...
1
by: Tim | last post by:
Folks, I have 3 loosely linked problems which I am would appreciate feedback on. 1). T-Sql and Active directory roles. We want to be able to control access to data within a table based on a...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

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.