473,326 Members | 2,438 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,326 software developers and data experts.

Thread question : Detecting application closing in a thread ?

Hi

If i have a form application that creates a thread, is there a way to detect
from within that thread if i have closed the form ?

Now i use the forms closing event to end the thread i started, but i would
like to do it automatcally so i do not have to worry about any threads that
remain active after i closed the form.
Johan

Nov 17 '05 #1
3 2541
The way I usually approach this is to have a static thread safe class which
contains a "still running" type flag that all spawned threads often check,
and which the main thread is able to set.

I'd recommend checking out Jon Skeet's threading pages for more
information...

http://www.yoda.arachsys.com/csharp/threads/

Thanks.
Dan.

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP09.phx.gbl...
Hi

If i have a form application that creates a thread, is there a way to
detect
from within that thread if i have closed the form ?

Now i use the forms closing event to end the thread i started, but i would
like to do it automatcally so i do not have to worry about any threads
that
remain active after i closed the form.
Johan

Nov 17 '05 #2
Solved it :
:
In my thread i check the active form.

if (Form.ActiveForm==null)
running=false;

So i don't have to shutdown the threads explicit from my main application.

"Dan Bass" <Not Listed> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...
The way I usually approach this is to have a static thread safe class which contains a "still running" type flag that all spawned threads often check,
and which the main thread is able to set.

I'd recommend checking out Jon Skeet's threading pages for more
information...

http://www.yoda.arachsys.com/csharp/threads/

Thanks.
Dan.

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP09.phx.gbl...
Hi

If i have a form application that creates a thread, is there a way to
detect
from within that thread if i have closed the form ?

Now i use the forms closing event to end the thread i started, but i would like to do it automatcally so i do not have to worry about any threads
that
remain active after i closed the form.
Johan


Nov 17 '05 #3
Ideally it would be good for the main for to have a stop method, that is
called on exiting the application, which has handles to all the threads.
This method sets the flag to false (as you have here) then waits for a
period for the threads to exit by checking their running state.
The threads are then responsible for checking the running state of the flag
after every couple of seconds, and if it is false, then stop running.

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP14.phx.gbl...
Solved it :
:
In my thread i check the active form.

if (Form.ActiveForm==null)
running=false;

So i don't have to shutdown the threads explicit from my main application.

"Dan Bass" <Not Listed> wrote in message
news:OC**************@TK2MSFTNGP10.phx.gbl...
The way I usually approach this is to have a static thread safe class

which
contains a "still running" type flag that all spawned threads often
check,
and which the main thread is able to set.

I'd recommend checking out Jon Skeet's threading pages for more
information...

http://www.yoda.arachsys.com/csharp/threads/

Thanks.
Dan.

"Sagaert Johan" <RE*************@hotmail.com> wrote in message
news:O%****************@TK2MSFTNGP09.phx.gbl...
> Hi
>
> If i have a form application that creates a thread, is there a way to
> detect
> from within that thread if i have closed the form ?
>
> Now i use the forms closing event to end the thread i started, but i would > like to do it automatcally so i do not have to worry about any threads
> that
> remain active after i closed the form.
>
>
> Johan
>
>
>



Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Byron | last post by:
I'm new to C# and threading, so hopefully this is a simple newbie question. I have a form that is supposed to listen for network traffic on a given port and decode and display any interesting...
4
by: Dr. J | last post by:
How to terminate a blocked thread? In my form's "load" I launch a TCP listening thread that stays in an infinite loop waiting for incoming TCP packets. In this form's "closing" I try to...
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
2
by: Paul Steele | last post by:
Some time ago I tracked down the code for detecting the shutdown event within a C# program. I tested it, it worked, and I moved on. However, I just discovered that the code is no longer working,...
1
by: Tuong | last post by:
I have a situation where i have a form that contains a webbrowser control. With this I was able to implement an application that can browse websites. One particular website i visited opens up...
5
by: taylorjonl | last post by:
I am completely baffled. I am writting a daemon application for my work to save me some time. The application works fine at my home but won't work right here at work. Basically I have a...
4
by: mammen | last post by:
Hello, I'm writing trace log of various actions happening in my ASP.NET web application by opening a text file when the user logs in to the system and closing the file while the user logs out....
9
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
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.