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.Current Thread.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 4 2464
>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.
"Guido Kraus" <gu*********@ne wsgroup.nospam> wrote in message
news:06******** *************** ***********@mic rosoft.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.Current Thread.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.
Thanks for your ideas.
I changed my code from Thread.CurrentT hread.Suspend() to
Thread.Sleep(Ti meout.Infinite)
To let it awake I use myThread.Interr upt().
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.Applicati on.ApplicationE xit 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*********@ne wsgroup.nospam> wrote in message news:06******** *************** ***********@mic rosoft.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.Current Thread.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.
Guido Kraus <gu*********@ne wsgroup.nospam> wrote: Thanks for your ideas. I changed my code from Thread.CurrentT hread.Suspend() to Thread.Sleep(Ti meout.Infinite) To let it awake I use myThread.Interr upt().
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.Applicati on.ApplicationE xit 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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 the synchronized method to get a synchronized Hashtable. The
documentation says that enumerating is not a thread safe operation; even
using a synchronized Hashtable. The Hashtable has a "ContainsKey" method, is
this method thread safe? Does the...
|
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 SDK (English) is
currently suspended. You must undo the changes made by that installation to
continue. Do you want to undo those changes?
|
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 suspended)
then sleep(100)
else
resume first Thread
After adding this code every thing is fine.
Its in C#.
So my question is When we suspend thread, is it suspended at that time only
|
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 at the ThreadState of
the thread, it does not show Suspended, but WaitSleepJoin.
Am I missing something between Thread.Suspend() and Thread.Resume()?
Thanks in advance,
Andy
|
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
| |
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 information: Thread is suspended; attempting to abort.
I have a number or threads in my program - some running, some suspended. I
hope I don't have to start the thread up again just to abort it??
|
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 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...
|
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 (through PInvoke'd Win32 calls) to
trigger an event, say, 30 seconds later. I create a timer, set its
properties, and then start a new thread to listen for the timer. However the
event triggers instantly, instead of waiting 30 seconds. Am I setting the...
|
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
role within Active directory.
For example, Region1 has 4 sites, Region 2 has 3 sites and so forth.
All the sites are held in a single database table.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |