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

Multiple Threads Within the IDE

I was noticing that when I run a multi-threaded app from within the IDE and
then I close the main form (which spun off all of the threads), that the
Stop/Pause VCR buttons in the debugger are still available & working. It's
like closing the form did not actually cancel the threads.

From my earlier discussions on the newsgroup, I know how people already feel
about Thread.Abort(), so I was wondering what I need to do to "clean up"
when closing a main form that has spun off worker threads (I was under the
impression that this cleanup would automatically happen, but seeing the
Stop/Pause VCR buttons on the debugger being active & working tell me
otherwise).

Any thoughts would be appreciated.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/


Nov 16 '05 #1
21 1592
Doug Thews <do*******@removeme.ddconsult.com> wrote:
I was noticing that when I run a multi-threaded app from within the IDE and
then I close the main form (which spun off all of the threads), that the
Stop/Pause VCR buttons in the debugger are still available & working. It's
like closing the form did not actually cancel the threads.
Indeed.
From my earlier discussions on the newsgroup, I know how people already feel
about Thread.Abort(), so I was wondering what I need to do to "clean up"
when closing a main form that has spun off worker threads (I was under the
impression that this cleanup would automatically happen, but seeing the
Stop/Pause VCR buttons on the debugger being active & working tell me
otherwise).


When you spin off the worker threads, make them background instead of
foreground threads. The CLR will shut down when all foreground threads
(eg your UI thread) have finished.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
well if you just want the trheads to terminate when teh main thread does set their IsBackground property to true. If you want the threads to come down in a controlled fashion, signal them (by an event, flag or Interrupt) and issue a Join on them before the form closes.

The main issue is that threads created uwing the Thread class will, by default, keep your process running.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Og**************@TK2MSFTNGP09.phx.gbl>

I was noticing that when I run a multi-threaded app from within the IDE and
then I close the main form (which spun off all of the threads), that the
Stop/Pause VCR buttons in the debugger are still available & working. It's
like closing the form did not actually cancel the threads.

From my earlier discussions on the newsgroup, I know how people already feel
about Thread.Abort(), so I was wondering what I need to do to "clean up"
when closing a main form that has spun off worker threads (I was under the
impression that this cleanup would automatically happen, but seeing the
Stop/Pause VCR buttons on the debugger being active & working tell me
otherwise).

Any thoughts would be appreciated.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]
Nov 16 '05 #3
So, what is the recommended method for shutting down threads spun off my a
main form in a WinFOrms app?

1. Create all worker threads by setting the IsBackground property to true?
2. Call Thread.Interrupt() followed by Thread.Join() within the Form's
Close event for all worker threads?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:es**************@TK2MSFTNGP12.phx.gbl...
well if you just want the trheads to terminate when teh main thread does
set their IsBackground property to true. If you want the threads to come
down in a controlled fashion, signal them (by an event, flag or Interrupt)
and issue a Join on them before the form closes.

The main issue is that threads created uwing the Thread class will, by
default, keep your process running.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<Og**************@TK2MSFTNGP09.phx.gbl>

I was noticing that when I run a multi-threaded app from within the IDE
and
then I close the main form (which spun off all of the threads), that the
Stop/Pause VCR buttons in the debugger are still available & working. It's
like closing the form did not actually cancel the threads.

From my earlier discussions on the newsgroup, I know how people already
feel
about Thread.Abort(), so I was wondering what I need to do to "clean up"
when closing a main form that has spun off worker threads (I was under the
impression that this cleanup would automatically happen, but seeing the
Stop/Pause VCR buttons on the debugger being active & working tell me
otherwise).

Any thoughts would be appreciated.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.771 / Virus Database: 518 - Release Date: 28/09/2004

[microsoft.public.dotnet.languages.csharp]

Nov 16 '05 #4
As with all important questions ... it depends. If the threads need to come down in a controlled fashion then the UI thread, once it exits from Application.Run can Join on the threads. If they don't need to come down in a controlled fashion but just need to terminate, make them background.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<uX*************@TK2MSFTNGP11.phx.gbl>

So, what is the recommended method for shutting down threads spun off my a
main form in a WinFOrms app?

1. Create all worker threads by setting the IsBackground property to true?
2. Call Thread.Interrupt() followed by Thread.Join() within the Form's
Close event for all worker threads?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

Nov 16 '05 #5
Any reason not to put the Interrupt() and Join() calls in the Form's Close
event handler?

BTW ... You guys have provided a lot of good information here - a lot more
than is show in the various threading articles and books (a lot of this IS
NEVER MENTIONED). Makes me think that an article written from the
beginner's perspective, but with THIS information, would be quite helpful.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:uB***************@TK2MSFTNGP12.phx.gbl...
As with all important questions ... it depends. If the threads need to
come down in a controlled fashion then the UI thread, once it exits from
Application.Run can Join on the threads. If they don't need to come down
in a controlled fashion but just need to terminate, make them background.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<uX*************@TK2MSFTNGP11.phx.gbl>

So, what is the recommended method for shutting down threads spun off my a
main form in a WinFOrms app?

1. Create all worker threads by setting the IsBackground property to true?
2. Call Thread.Interrupt() followed by Thread.Join() within the Form's
Close event for all worker threads?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

Nov 16 '05 #6
Doug Thews <do*******@removeme.ddconsult.com> wrote:
Any reason not to put the Interrupt() and Join() calls in the Form's Close
event handler?
I would suggest not using Interrupt, myself - it's not a terribly
nicely controlled way of doing things. I would have a property which is
polled regularly by each worker thread. The only downside of that is
that if a thread gets "stuck" it could hold the app up. To stop that
from being a problem, you could have an extra background thread which
*does* call Thread.Interrupt, or Thread.Abort, or even Environment.Exit
if it's still running say 5 seconds after the UI has shut down.

For code demonstrating what I mean about the polling, see
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

As for calling Join() - why bother? What would you want to do after
you'd waited for them? If you're just going to return, then let the
main thread just die and the other threads can die in their own time.
BTW ... You guys have provided a lot of good information here - a lot more
than is show in the various threading articles and books (a lot of this IS
NEVER MENTIONED). Makes me think that an article written from the
beginner's perspective, but with THIS information, would be quite helpful.


If you have any suggestions for how I can improve my article at
http://www.pobox.com/~skeet/csharp/threads I'd be keen to improve it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
My only tips would be to not rely on console apps (not much of a real-world
scenario) and move your thread code out from within the Form's class code
and into its own class (as you'd probably see in the real world). I found
out a bunch of scope stuff that changes once you try and do delegates off of
methods within classes, rather than just a method within the Form class.

I'd also recommend somehow that you get the word out about your article. It
doesn't hit very high in Google, and the one's that do are the articles I'm
complaining about right now - the ones that just show console apps with no
meat, no talk about how to safely update the UI from a worker thread, and
how to safely bring down worker threads.

Thanks for asking my opinion.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Doug Thews <do*******@removeme.ddconsult.com> wrote:
Any reason not to put the Interrupt() and Join() calls in the Form's
Close
event handler?


I would suggest not using Interrupt, myself - it's not a terribly
nicely controlled way of doing things. I would have a property which is
polled regularly by each worker thread. The only downside of that is
that if a thread gets "stuck" it could hold the app up. To stop that
from being a problem, you could have an extra background thread which
*does* call Thread.Interrupt, or Thread.Abort, or even Environment.Exit
if it's still running say 5 seconds after the UI has shut down.

For code demonstrating what I mean about the polling, see
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

As for calling Join() - why bother? What would you want to do after
you'd waited for them? If you're just going to return, then let the
main thread just die and the other threads can die in their own time.
BTW ... You guys have provided a lot of good information here - a lot
more
than is show in the various threading articles and books (a lot of this
IS
NEVER MENTIONED). Makes me think that an article written from the
beginner's perspective, but with THIS information, would be quite
helpful.


If you have any suggestions for how I can improve my article at
http://www.pobox.com/~skeet/csharp/threads I'd be keen to improve it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
Maybe I missed this in your article (but I don't think so), but ... Is there
a reason that a ThreadInterruptedException does not get raised in a worker
thread if that worker thread just loops without having a Thread.Sleep
command somewhere in the worker thread's loop (even if the Thread is marked
for background before it's started)?

I would've thought that a Sleep wasn't necessary when marking it as
IsBackground before starting it, but until I put at least a Thread.Sleep(0)
(thus giving up at least that timeslice within the loop), my
Thread.Interrupt() from the main thread never generates an exception within
the try...catch clause of my worker thread that surrounds the loop.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Doug Thews <do*******@removeme.ddconsult.com> wrote:
Any reason not to put the Interrupt() and Join() calls in the Form's
Close
event handler?


I would suggest not using Interrupt, myself - it's not a terribly
nicely controlled way of doing things. I would have a property which is
polled regularly by each worker thread. The only downside of that is
that if a thread gets "stuck" it could hold the app up. To stop that
from being a problem, you could have an extra background thread which
*does* call Thread.Interrupt, or Thread.Abort, or even Environment.Exit
if it's still running say 5 seconds after the UI has shut down.

For code demonstrating what I mean about the polling, see
http://www.yoda.arachsys.com/csharp/...shutdown.shtml

As for calling Join() - why bother? What would you want to do after
you'd waited for them? If you're just going to return, then let the
main thread just die and the other threads can die in their own time.
BTW ... You guys have provided a lot of good information here - a lot
more
than is show in the various threading articles and books (a lot of this
IS
NEVER MENTIONED). Makes me think that an article written from the
beginner's perspective, but with THIS information, would be quite
helpful.


If you have any suggestions for how I can improve my article at
http://www.pobox.com/~skeet/csharp/threads I'd be keen to improve it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #9
Hi Doug:

The thread cannot be interrupted until it blocks (ThreadState =
WaitSleepJoin).

--
Scott
http://www.OdeToCode.com/

On Fri, 8 Oct 2004 21:04:27 -0500, "Doug Thews"
<do*******@removeme.ddconsult.com> wrote:
Maybe I missed this in your article (but I don't think so), but ... Is there
a reason that a ThreadInterruptedException does not get raised in a worker
thread if that worker thread just loops without having a Thread.Sleep
command somewhere in the worker thread's loop (even if the Thread is marked
for background before it's started)?

I would've thought that a Sleep wasn't necessary when marking it as
IsBackground before starting it, but until I put at least a Thread.Sleep(0)
(thus giving up at least that timeslice within the loop), my
Thread.Interrupt() from the main thread never generates an exception within
the try...catch clause of my worker thread that surrounds the loop.


Nov 16 '05 #10
Doug Thews <do*******@removeme.ddconsult.com> wrote:
My only tips would be to not rely on console apps (not much of a real-world
scenario) and move your thread code out from within the Form's class code
and into its own class (as you'd probably see in the real world). I found
out a bunch of scope stuff that changes once you try and do delegates off of
methods within classes, rather than just a method within the Form class.
I'll certainly do the latter - that's a good point. I prefer console
apps for demonstration purposes because they don't involve a load of
extra "cruft" - virtually all the code you see is relevant to the point
I'm trying to demonstrate. (Almost all the samples are doing non-real-
world things, but to give building blocks which will help in the real
world. In my experience, sample code which tries to be very real-world
oriented ends up being either confusing or useful for anything other
than that oe particular situation.) Maybe an explanatory paragraph
saying that kind of thing would be helpful.
I'd also recommend somehow that you get the word out about your article. It
doesn't hit very high in Google, and the one's that do are the articles I'm
complaining about right now - the ones that just show console apps with no
meat, no talk about how to safely update the UI from a worker thread, and
how to safely bring down worker threads.


Right. Word is gradually spreading about the article, and I'll keep
plugging it when there are questions about it. Not entirely sure where
else to "advertise" it, to be honest - but any ideas are welcome.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11
Interrupt will only throw the exception when the thread is in an alertable state (Sleeping, waiting on a synchronization primitive or attempting to joion with another thread). This is how it ensures that the exception doesn't do the damage that Abort can do.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<##**************@tk2msftngp13.phx.gbl>

Maybe I missed this in your article (but I don't think so), but ... Is there
a reason that a ThreadInterruptedException does not get raised in a worker
thread if that worker thread just loops without having a Thread.Sleep
command somewhere in the worker thread's loop (even if the Thread is marked
for background before it's started)?

I would've thought that a Sleep wasn't necessary when marking it as
IsBackground before starting it, but until I put at least a Thread.Sleep(0)
(thus giving up at least that timeslice within the loop), my
Thread.Interrupt() from the main thread never generates an exception within
the try...catch clause of my worker thread that surrounds the loop.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/
Nov 16 '05 #12
A thread ends when it exists the function that it is executing. If the
thread performs a continuos loop you usually use a boolean to control the
execution, something like:

bool run;

void Foo()
{
while( run )
{
// perform whatever
}
}

So what I do to stop and end the thread in a clean way is:

run = false;

this makes the executuion flow to exit the function and end the thread.

regards,
Santiago Corredoira.
Nov 16 '05 #13
How about on your blog, and then try and get references to it on some of the
other major blogs (like Scobleizer, etc).

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Doug Thews <do*******@removeme.ddconsult.com> wrote:
My only tips would be to not rely on console apps (not much of a
real-world
scenario) and move your thread code out from within the Form's class code
and into its own class (as you'd probably see in the real world). I
found
out a bunch of scope stuff that changes once you try and do delegates off
of
methods within classes, rather than just a method within the Form class.


I'll certainly do the latter - that's a good point. I prefer console
apps for demonstration purposes because they don't involve a load of
extra "cruft" - virtually all the code you see is relevant to the point
I'm trying to demonstrate. (Almost all the samples are doing non-real-
world things, but to give building blocks which will help in the real
world. In my experience, sample code which tries to be very real-world
oriented ends up being either confusing or useful for anything other
than that oe particular situation.) Maybe an explanatory paragraph
saying that kind of thing would be helpful.
I'd also recommend somehow that you get the word out about your article.
It
doesn't hit very high in Google, and the one's that do are the articles
I'm
complaining about right now - the ones that just show console apps with
no
meat, no talk about how to safely update the UI from a worker thread, and
how to safely bring down worker threads.


Right. Word is gradually spreading about the article, and I'll keep
plugging it when there are questions about it. Not entirely sure where
else to "advertise" it, to be honest - but any ideas are welcome.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #14
So, when the OS interrupts the thread because it's gone past its timeslice,
it's not in the WaitSleepJoin state? What state would the thread be in when
the OS is the one who pre-empts it to run other stuff?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:b1********************************@4ax.com...
Hi Doug:

The thread cannot be interrupted until it blocks (ThreadState =
WaitSleepJoin).

--
Scott
http://www.OdeToCode.com/

On Fri, 8 Oct 2004 21:04:27 -0500, "Doug Thews"
<do*******@removeme.ddconsult.com> wrote:
Maybe I missed this in your article (but I don't think so), but ... Is
there
a reason that a ThreadInterruptedException does not get raised in a worker
thread if that worker thread just loops without having a Thread.Sleep
command somewhere in the worker thread's loop (even if the Thread is
marked
for background before it's started)?

I would've thought that a Sleep wasn't necessary when marking it as
IsBackground before starting it, but until I put at least a
Thread.Sleep(0)
(thus giving up at least that timeslice within the loop), my
Thread.Interrupt() from the main thread never generates an exception
within
the try...catch clause of my worker thread that surrounds the loop.

Nov 16 '05 #15
Thread.Interrupt is a 'soft interrupt', probably implemented by
setting a flag the runtime can check at specific times (before a
thread enters a blocked state, for instance). In fact, the name
interrupt might be a poor choice, except for the fact that we are so
far removed from interrupts with a managed runtime underneath.

The OS uses hardware (the clock) to interrupt a running thread and
make scheduling decisions. If the OS selects another thread it can
perform a context switch, which is saving the current thread's state
(program counter, registers, and other goo) onto the thread's kernel
mode stack, and restoring the same information from the second
thread's kernel mode stack. That's the simplest view, of course.

--
Scott
http://www.OdeToCode.com/

On Sat, 9 Oct 2004 11:19:12 -0500, "Doug Thews"
<do*******@removeme.ddconsult.com> wrote:
So, when the OS interrupts the thread because it's gone past its timeslice,
it's not in the WaitSleepJoin state? What state would the thread be in when
the OS is the one who pre-empts it to run other stuff?


Nov 16 '05 #16
You're looking at this slightly too low level. What the thread schedular is doing is to some degree irrelevant to you unless you want to give up your timeslice, change priorities, etc.

The crucial thing is to think of the situation as having an infinite number of processors available. In other words your thread will appear to other threads in the state it would be if it were being scheduled (in other words thread scheduling has no effect on your thread's state).

If you try to second guess the thread scheduler its a sure fire way of introducing timing related bugs.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<OG**************@TK2MSFTNGP09.phx.gbl>

So, when the OS interrupts the thread because it's gone past its timeslice,
it's not in the WaitSleepJoin state? What state would the thread be in when
the OS is the one who pre-empts it to run other stuff?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/
Nov 16 '05 #17
OK, thanks. I wasn't expecting to have to give up my timeslice periodically
just to allow a ThreadInterruptedException to be generated the next time my
thread gets a timeslice, but I understand what you guys are trying to say.

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Richard Blewett [DevelopMentor]" <ri******@develop.com> wrote in message
news:u8**************@TK2MSFTNGP11.phx.gbl...
You're looking at this slightly too low level. What the thread schedular
is doing is to some degree irrelevant to you unless you want to give up
your timeslice, change priorities, etc.

The crucial thing is to think of the situation as having an infinite
number of processors available. In other words your thread will appear to
other threads in the state it would be if it were being scheduled (in
other words thread scheduling has no effect on your thread's state).

If you try to second guess the thread scheduler its a sure fire way of
introducing timing related bugs.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<OG**************@TK2MSFTNGP09.phx.gbl>

So, when the OS interrupts the thread because it's gone past its
timeslice,
it's not in the WaitSleepJoin state? What state would the thread be in
when
the OS is the one who pre-empts it to run other stuff?

--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

Nov 16 '05 #18
Santi <__***********@ibcentre.es> wrote:
A thread ends when it exists the function that it is executing. If the
thread performs a continuos loop you usually use a boolean to control the
execution, something like:

bool run;

void Foo()
{
while( run )
{
// perform whatever
}
}

So what I do to stop and end the thread in a clean way is:

run = false;

this makes the executuion flow to exit the function and end the thread.


Note that as written, this isn't thread-safe. Either run should be made
volatile, or you should put a lock round both the reading and writing
of it.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19
Doug Thews <do*******@removeme.ddconsult.com> wrote:
How about on your blog, and then try and get references to it on some of the
other major blogs (like Scobleizer, etc).


I don't have a blog - but people probably see more references to the
article in the newsgroups than would read a blog anyway :)

I guess I could see whether any other blogs fancied commenting on it.
I'll have a think - thanks for the suggestion :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #20
Agreed. It's probably better to just use "true" instead of a variable, and
then catch the Interrupted exception (if you want a thread that runs
continuously and only quits when the app quits - as I was suggesting):

try
{
while( true )
{
// perform whatever
}
}
catch (ThreadInterruptedException)
{
// Perform cleanup - then return
return;
}
--
Doug Thews
Director, Customer Solutions
D&D Consulting Services
----------------
Visit my Tech Blog at:
http://www.ddconsult.com/blogs/illuminati/

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
Santi <__***********@ibcentre.es> wrote:
A thread ends when it exists the function that it is executing. If the
thread performs a continuos loop you usually use a boolean to control the
execution, something like:

bool run;

void Foo()
{
while( run )
{
// perform whatever
}
}

So what I do to stop and end the thread in a clean way is:

run = false;

this makes the executuion flow to exit the function and end the thread.


Note that as written, this isn't thread-safe. Either run should be made
volatile, or you should put a lock round both the reading and writing
of it.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #21
Doug Thews <do*******@removeme.ddconsult.com> wrote:
Agreed. It's probably better to just use "true" instead of a variable, and
then catch the Interrupted exception (if you want a thread that runs
continuously and only quits when the app quits - as I was suggesting):

try
{
while( true )
{
// perform whatever
}
}
catch (ThreadInterruptedException)
{
// Perform cleanup - then return
return;
}


No, I don't agree with that. It means that you don't really know where
you were when you were interrupted. I prefer to still use an orderly
stopping procedure, but do it in a thread-safe way.

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

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

Similar topics

6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
4
by: Tony Liu | last post by:
Hi, how can I create multiple new file handles of a file without having to share to file to the other processes? I have a file that will be accessed by multiple threads in my application, each...
6
by: Quiet Man | last post by:
Hi all, I'm designing a fairly simple service that will run on W2K/SP4 and W2K3 servers. It's job is to be a very specialized database server that listens on a given IP address / TCP port and...
2
by: Oenone | last post by:
I'm upgrading a DLL from VB6 to VB.NET. The DLL gets called from an ASP.NET web application. In the VB6 code there is a module-level object which stores the context about what the user is doing...
2
by: Tumurbaatar S. | last post by:
ASP.NET QuickStart Tutorial says that: .... ASP.NET maintains a pool of HttpApplication instances over the course of a Web application's lifetime. ASP.NET automatically assigns one of these...
4
by: gnassar | last post by:
Hello, I've written to this group a couple times and would like to initially start by thanking those who reply to the posts. I seem to be having some issues that are out of my understanding at...
6
by: Milsnips | last post by:
hi there, i created a little test application to send out multiple emails to addresses, but using threads. So anyway, what i've got is 1 form with a START button, and a multiline textbox for...
7
by: skip | last post by:
This question was posed to me today. Given a C/C++ program we can clearly embed a Python interpreter in it. Is it possible to fire up multiple interpreters in multiple threads? For example: ...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
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,...
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
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.