473,397 Members | 1,960 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,397 software developers and data experts.

Terminating thread

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);
}

The problem is that I don't know how to terminate the thread when my class
is destroyed. When closing application, destructor of my class is NOT called
so I can't stop thread there. Looks like destructor will be called AFTER the
thread has finished. Interesting and I'm stuck here because I don't know
where to put the code that stops the thread. Also, I want my class to stop
thread automatically upon destruction and I don't want to call some method
of my class from outside to stop the thread.

I can't believe that I can't solve this problem because doing it in Delphi
it's a child's play :).

regards
Tomaz
Nov 17 '05 #1
6 23692
Hi Tomaz,

I am not sure whether your problem is as complicated as it sounds.

Perhaps your problem is just that your thread won't terminate when the
application terminates. If this is the case, then you need the IsBackground
property...

Thread myThread = new Thread(...);
myThread.IsBackground = true;
myThread.Start();

Now your thread will terminate when the application terminates.

However, perhaps your problem is more complicated...

- You have a class which creates a thread
- Your application creates an instance of the class
- Your application deletes the instance
- Your application continues running
- You thread also continues running. Problem! You expect the thread to have
been stopped.

I have done some prototyping, and found that I cannot solve this problem. I
have created a finalizer for the class, which stops the thread, as follows:

class ThreadClass
{
....

volatile bool run = false;

~ThreadClass()
{
run = false;
}

void ThreadEntry()
{
run = true;
while (run)
{
Thread.Sleep(300);
}
}

}

However, I have been unable to force the finalizer to be called. I have
tried to make the instance available for collection, by setting it to null,
and then forcing garbage collection with GC.Collect(), but this hasn't
worked. The problem here is not terminating the thread, but getting the
finalizer to be called.

I suggest that you stop the thread from outside, by making a public method
on the thread class...

class ThreadClass
{
.....
void Stop()
{
run = false;
}
}

HTH,

Javaman

"Tomaz Koritnik" wrote:
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);
}

The problem is that I don't know how to terminate the thread when my class
is destroyed. When closing application, destructor of my class is NOT called
so I can't stop thread there. Looks like destructor will be called AFTER the
thread has finished. Interesting and I'm stuck here because I don't know
where to put the code that stops the thread. Also, I want my class to stop
thread automatically upon destruction and I don't want to call some method
of my class from outside to stop the thread.

I can't believe that I can't solve this problem because doing it in Delphi
it's a child's play :).

regards
Tomaz


Nov 17 '05 #2
Your example shows exactly the same problem I have. I solved it using
IsBackground = true (hope this doesn't bring other bugs) but still I'm
curious how to stop thread without "sending it to background". I don't want
to declare public method because I want my class to control thread life. If
at least there would exists some flag somewhere (in application or in thread
itself) that will be set before app terminates.

Thanks for trying it out.

regards
Tomaz
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
Hi Tomaz,

I am not sure whether your problem is as complicated as it sounds.

Perhaps your problem is just that your thread won't terminate when the
application terminates. If this is the case, then you need the
IsBackground
property...

Thread myThread = new Thread(...);
myThread.IsBackground = true;
myThread.Start();

Now your thread will terminate when the application terminates.

However, perhaps your problem is more complicated...

- You have a class which creates a thread
- Your application creates an instance of the class
- Your application deletes the instance
- Your application continues running
- You thread also continues running. Problem! You expect the thread to
have
been stopped.

I have done some prototyping, and found that I cannot solve this problem.
I
have created a finalizer for the class, which stops the thread, as
follows:

class ThreadClass
{
...

volatile bool run = false;

~ThreadClass()
{
run = false;
}

void ThreadEntry()
{
run = true;
while (run)
{
Thread.Sleep(300);
}
}

}

However, I have been unable to force the finalizer to be called. I have
tried to make the instance available for collection, by setting it to
null,
and then forcing garbage collection with GC.Collect(), but this hasn't
worked. The problem here is not terminating the thread, but getting the
finalizer to be called.

I suggest that you stop the thread from outside, by making a public method
on the thread class...

class ThreadClass
{
....
void Stop()
{
run = false;
}
}

HTH,

Javaman

"Tomaz Koritnik" wrote:
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);
}

The problem is that I don't know how to terminate the thread when my
class
is destroyed. When closing application, destructor of my class is NOT
called
so I can't stop thread there. Looks like destructor will be called AFTER
the
thread has finished. Interesting and I'm stuck here because I don't know
where to put the code that stops the thread. Also, I want my class to
stop
thread automatically upon destruction and I don't want to call some
method
of my class from outside to stop the thread.

I can't believe that I can't solve this problem because doing it in
Delphi
it's a child's play :).

regards
Tomaz

Nov 17 '05 #3
Ron
You are going to need to send a message to the class so that it stops the
thread, which means you will need to call a method outside your class, like
Dispose (if you don't want the background thread). The destructor will not
get called if a thread started from that class is still running, and your
application will wait for that thread to finish too. I'm afraid those are the
rules of this game.

"Tomaz Koritnik" wrote:
Your example shows exactly the same problem I have. I solved it using
IsBackground = true (hope this doesn't bring other bugs) but still I'm
curious how to stop thread without "sending it to background". I don't want
to declare public method because I want my class to control thread life. If
at least there would exists some flag somewhere (in application or in thread
itself) that will be set before app terminates.

Thanks for trying it out.

regards
Tomaz
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:61**********************************@microsof t.com...
Hi Tomaz,

I am not sure whether your problem is as complicated as it sounds.

Perhaps your problem is just that your thread won't terminate when the
application terminates. If this is the case, then you need the
IsBackground
property...

Thread myThread = new Thread(...);
myThread.IsBackground = true;
myThread.Start();

Now your thread will terminate when the application terminates.

However, perhaps your problem is more complicated...

- You have a class which creates a thread
- Your application creates an instance of the class
- Your application deletes the instance
- Your application continues running
- You thread also continues running. Problem! You expect the thread to
have
been stopped.

I have done some prototyping, and found that I cannot solve this problem.
I
have created a finalizer for the class, which stops the thread, as
follows:

class ThreadClass
{
...

volatile bool run = false;

~ThreadClass()
{
run = false;
}

void ThreadEntry()
{
run = true;
while (run)
{
Thread.Sleep(300);
}
}

}

However, I have been unable to force the finalizer to be called. I have
tried to make the instance available for collection, by setting it to
null,
and then forcing garbage collection with GC.Collect(), but this hasn't
worked. The problem here is not terminating the thread, but getting the
finalizer to be called.

I suggest that you stop the thread from outside, by making a public method
on the thread class...

class ThreadClass
{
....
void Stop()
{
run = false;
}
}

HTH,

Javaman

"Tomaz Koritnik" wrote:
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);
}

The problem is that I don't know how to terminate the thread when my
class
is destroyed. When closing application, destructor of my class is NOT
called
so I can't stop thread there. Looks like destructor will be called AFTER
the
thread has finished. Interesting and I'm stuck here because I don't know
where to put the code that stops the thread. Also, I want my class to
stop
thread automatically upon destruction and I don't want to call some
method
of my class from outside to stop the thread.

I can't believe that I can't solve this problem because doing it in
Delphi
it's a child's play :).

regards
Tomaz


Nov 17 '05 #4
why not use some boolean class-level variable such as "bRun"

Before kicking off your thread, you can set this to true.

When you are ready to stop, just set it to false.

So ,

while (bRun)
{
// your code

}

When you set bRun to false, the loop will terminate.

Peter

"Tomaz Koritnik" <no****@nospam.com> wrote in message
news:x0****************@news.siol.net...
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);
}

The problem is that I don't know how to terminate the thread when my class
is destroyed. When closing application, destructor of my class is NOT
called so I can't stop thread there. Looks like destructor will be called
AFTER the thread has finished. Interesting and I'm stuck here because I
don't know where to put the code that stops the thread. Also, I want my
class to stop thread automatically upon destruction and I don't want to
call some method of my class from outside to stop the thread.

I can't believe that I can't solve this problem because doing it in Delphi
it's a child's play :).

regards
Tomaz

Nov 17 '05 #5
As is:

private void cout_thread()
{
try
{
// if process does not exit normally, will
// throw exception on close
while(!myProcess.HasExited)
{
your_cout.WriteLine(my_cout.ReadLine());
}
your_cout.WriteLine(my_cout.ReadToEnd());
}
catch (Exception e) {}
finally
{
notifyCoutDone();
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
Peter Bromberg [C# MVP] <pb*******@yahoo.spamsucks.com> wrote:
why not use some boolean class-level variable such as "bRun"

Before kicking off your thread, you can set this to true.

When you are ready to stop, just set it to false.

So ,

while (bRun)
{
// your code

}

When you set bRun to false, the loop will terminate.


Only if bRun is volatile. Otherwise it just *might* terminate. See
http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

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

Similar topics

12
by: Jerry Sievers | last post by:
Greetings Pythonists; I have limited experience with threaded apps and plenty with old style forked heavyweight multi-processing apps. Using Python 2.3.3 on a Redhat 7.x machine. Wondering...
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...
8
by: Gaetan | last post by:
Hi let this code : using System; using System.Threading; namespace ThisNamespace { class ThisClass
7
by: Edwin | last post by:
Hello, I would like the Main()-thread to end (because it runs out of the code), but all started threads should continue. Is this possible. Eg. static void Main(string args) {
9
by: Li Pang | last post by:
Hi I make an app which can run some sub processes through multiple threads. I'd like to know how to terminate all sub-threads when the main thread is closed thanks in advance
0
by: Sir Spamallot | last post by:
Hi there, Previously when handling the termination of threads I had just called the abort method and caught it in a try catch block in the thread callback. After recently learning that this was...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
2
by: PhotoLover | last post by:
I have gotten 3 messages saying, "Terminating thread due to stack overflow problems. A V&D, possibly recently installed has consumed too much stack space. Increase the settings of MinSP's in...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, I have a thread ABC that starts another thread XYX. Thread XYZ monitors various things and if there is no work to do it calls Thread.Sleep to sleep for a minute or so. Occasionally...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
agi2029
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,...
0
isladogs
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 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.