473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2428
>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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.