By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,062 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Terminating thread

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.