473,834 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1639
Doug Thews <do*******@remo veme.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.co m>
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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.Interrup t() 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******@devel op.com> wrote in message
news:es******** ******@TK2MSFTN GP12.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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.publi c.dotnet.langua ges.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.Interrup t() 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******@devel op.com> wrote in message
news:uB******** *******@TK2MSFT NGP12.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.publi c.dotnet.langua ges.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.Interrup t() 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*******@remo veme.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.Interrup t, or Thread.Abort, or even Environment.Exi t
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.co m>
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.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com...
Doug Thews <do*******@remo veme.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.Interrup t, or Thread.Abort, or even Environment.Exi t
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.co m>
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 ThreadInterrupt edException 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.Interrup t() 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.co m> wrote in message
news:MP******** *************** @msnews.microso ft.com...
Doug Thews <do*******@remo veme.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.Interrup t, or Thread.Abort, or even Environment.Exi t
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.co m>
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*******@remo veme.ddconsult. com> wrote:
Maybe I missed this in your article (but I don't think so), but ... Is there
a reason that a ThreadInterrupt edException 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.Interru pt() 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

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

Similar topics

6
3209
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 list within a single object, which is then placed on a stack(This cuts down thread creation and deletions roughly by a factor of 4). I create up to 12 threads, which then process a single object off of the stack. I use a loop with a boolean...
4
5305
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 time a thread try to do something with the file, the thread will create a new file handle. However, if I specify FileShare.ReadWrite, other process can also open that file. I tried FileShare.Inheritable but it doesn't work. The reason I needs to...
6
4452
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 handles multiple connections. Client programs will make a connection and pass text strings to the service, which will then return a value for each of the strings. It's unlikely the service will ever have more than 100 simultaneous connections and...
2
1811
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 during each page request. This object is initialised at the start of each request and populated with various data retrieved from the URL, and is then repeatedly queried throughout the rest of the page generation. Am I right in thinking that...
2
2106
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 instances to process each incoming HTTP request that is received by the application. The particular HttpApplication instance assigned is responsible for managing the entire lifetime of the request and is reused only after the request has been...
4
1307
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 the moment and hopefully someone knowledgeable will be able to guide me the right way. I have created a program that utilizes three threads minimum. The initial gui thread, and two worker threads. I haven't set the worker threads as background...
6
3054
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 recording log output of sending results. I use the code: System.Threading.Thread t = new System.Threading.Thread(new
7
12598
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: C++ main thread 1 Py_Initialize() thread 2 Py_Initialize()
4
6477
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 nearly so). I researched this and for C# (as opposed to MFC) there is no library function, and no easy way, though some code on the net suggested that you set up a thread that 'lives' for a certain time, then, if keys are pressed in that certain...
0
9799
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10793
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10510
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10548
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7758
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6954
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5794
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4427
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 we have to send another system

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.