Hi
let this code :
using System;
using System.Threading;
namespace ThisNamespace
{
class ThisClass
{
static void Main()
{
new ThisClass();
// here the end of the application => should call the
"destructor"
}
public ThisClass()
{
StartThread();
}
// finaliser
~ThisClass()
{
_WorkingThread.Abort();
}
Thread _WorkingThread;
public void StartThread()
{
_WorkingThread = new Thread( new ThreadStart(DoWork));
_WorkingThread.Start();
// if you remove this Start(), the "destructor" is well
called...
}
void DoWork()
{
while(true)
;
}
}
}
This is a simple example of what i would like to do: a class that
launch an other thread in this constructor, this thread doint whatever
it wants.
But when the application is closed, the thread should be destroyed
(properly by calling _WorkingThread.Abort in the "destructor"). But
this destructor is not called and so, I cannot stop my thread, and
this result with an executable still launched !!!
Any idea please?
PS: and please, no ThreadPool based answer... 8 2121
Hi,
1. The destructor will be called WHEN garbage collector destroys the
ThisClass object. You can not predict it when, so maybe GC.Collect may
help.
2. Thread.Abort is not very good method for terminating a thread.
3. You can set your thread to be background just before starting it,
then it will be terminated upon application exit.
Sunny
In article <97**************************@posting.google.com >, ga****@xeberon.net says... Hi
let this code :
using System; using System.Threading;
namespace ThisNamespace { class ThisClass { static void Main() { new ThisClass(); // here the end of the application => should call the "destructor" }
public ThisClass() { StartThread(); }
// finaliser ~ThisClass() { _WorkingThread.Abort(); }
Thread _WorkingThread;
public void StartThread() { _WorkingThread = new Thread( new ThreadStart(DoWork)); _WorkingThread.Start(); // if you remove this Start(), the "destructor" is well called... }
void DoWork() { while(true) ; } } }
This is a simple example of what i would like to do: a class that launch an other thread in this constructor, this thread doint whatever it wants. But when the application is closed, the thread should be destroyed (properly by calling _WorkingThread.Abort in the "destructor"). But this destructor is not called and so, I cannot stop my thread, and this result with an executable still launched !!!
Any idea please?
PS: and please, no ThreadPool based answer...
Sunny <su******@icebergwireless.com> wrote in message news:<eJ**************@TK2MSFTNGP11.phx.gbl>... Hi,
1. The destructor will be called WHEN garbage collector destroys the ThisClass object. You can not predict it when, so maybe GC.Collect may help.
2. Thread.Abort is not very good method for terminating a thread.
3. You can set your thread to be background just before starting it, then it will be terminated upon application exit.
Thank you very much.
So what is your advise for terminating a thread? I mean, when the
application will close, the thread should close by themself... I can
add a terminated check into the threads, but I need a way to tell the
thread it should end... how to do that? By recording all the thread
into an internal list? I hoped the destructor (ah.... i miss my lovely
c++ destructors :) ) should do that...
Gaetan
Hi, Gaetan
You can return from thread method body on termination check. This will
effectively terminate the thread. You can tell thread to terminate using
global flag, event or variety of other techniques. What is exactly your
problem?
HTH
Alex
"Gaetan" <ga****@xeberon.net> wrote in message
news:97**************************@posting.google.c om... Sunny <su******@icebergwireless.com> wrote in message
news:<eJ**************@TK2MSFTNGP11.phx.gbl>... Hi,
1. The destructor will be called WHEN garbage collector destroys the ThisClass object. You can not predict it when, so maybe GC.Collect may help.
2. Thread.Abort is not very good method for terminating a thread.
3. You can set your thread to be background just before starting it, then it will be terminated upon application exit. Thank you very much. So what is your advise for terminating a thread? I mean, when the application will close, the thread should close by themself... I can add a terminated check into the threads, but I need a way to tell the thread it should end... how to do that? By recording all the thread into an internal list? I hoped the destructor (ah.... i miss my lovely c++ destructors :) ) should do that...
Gaetan
Hi Gaetan,
In article <97**************************@posting.google.com >, ga****@xeberon.net says... Thank you very much. So what is your advise for terminating a thread? I mean, when the application will close, the thread should close by themself... I can add a terminated check into the threads, but I need a way to tell the thread it should end... how to do that? By recording all the thread into an internal list? I hoped the destructor (ah.... i miss my lovely c++ destructors :) ) should do that...
Gaetan
As AlexS already stated, you can make some global (or local) flag, and
in your thread you can check if the thread have to end.
using System;
using System.Threading;
namespace ThisNamespace
{
class ThisClass
{
static void Main()
{
ThisClass mythread = new ThisClass();
mythread.StopThread();
}
public ThisClass()
{
StartThread();
}
Thread _WorkingThread;
bool keepAlive;
public void StartThread()
{
_WorkingThread = new Thread( new ThreadStart(DoWork));
keepAlive = true;
_WorkingThread.Start();
}
void DoWork()
{
while(keepAlive)
;
}
public void StopThread()
{
keepAlive = false;
}
}
}
Sunny
Sunny <su******@icebergwireless.com> wrote: As AlexS already stated, you can make some global (or local) flag, and in your thread you can check if the thread have to end.
<snip>
Note that the code you provided wasn't thread-safe.
See http://www.pobox.com/~skeet/csharp/m...worker.threads
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Yes, I know. I just edited his example to make it work the way OP
wanted.
Thanks for the note anyway.
Sunny
In article <MP************************@msnews.microsoft.com >, sk***@pobox.com says... Sunny <su******@icebergwireless.com> wrote: As AlexS already stated, you can make some global (or local) flag, and in your thread you can check if the thread have to end.
<snip>
Note that the code you provided wasn't thread-safe.
See http://www.pobox.com/~skeet/csharp/m...worker.threads
Thanks all. That helped me a lot ! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Daylor |
last post by:
in win32 process , when u create new process,u have new main thread.
i know,appDomain r logical procces,that exists in 1 win32 process.
the q:
is there way to create second appDomain (the...
|
by: Tony Proctor |
last post by:
Hi everyone
We're experiencing some serious anomalies with the scheduling of ASP
threads. I'd be interested to hear if anyone knows what algorithm is used
(e.g. simple round-robin, or something...
|
by: Serge |
last post by:
Hi,
I am having a thread hang problem in my c# code.
The example on the website: http://csharp.web1000.com/ is a
simplified version of my problem.
You will see in the form that a...
|
by: droopytoon |
last post by:
Hi,
I start a new thread (previous one was "thread timing") because I have
isolated my problem.
It has nothing to do with calling unmanaged C++ code (I removed it in a
test application).
I...
|
by: Tomaz Koritnik |
last post by:
I have a class that runs one of it's method in another thread. I use Thread
object to do this and inside ThreadMethod I have an infinite loop:
While (true)
{
// do something
Thread.Sleep(100);...
| |
by: Bob Day |
last post by:
Using vs2003, vb.net
I start a thread, giving it a name before start. Code snippet:
'give each thread a unique name (for later identification)
Trunk_Thread.Name = "Trunk_0_Thread"
' allow...
|
by: alundi |
last post by:
Greetings:
I'd like to post this as a new thread to an article in
microsoft.public.dotnet.languages.vb originally made by nevin and replied to
by Alan Pretre back in December ("ThreadState ==...
|
by: Morpheus |
last post by:
Hi,
I have been coding in Windows for many years so have a mindset to it,
so forgive any stupid questions.
Is it possible to create a multithread application in C++ that is
portable...
|
by: mkashif1 |
last post by:
Hi,
I'm developing a service that will be monitoring some of the
activities of currently logged in user.
one of the requirement is that the service must be unstoppable. The
user (any user even...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |