473,950 Members | 47,285 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
21 1655
Doug Thews <do*******@remo veme.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.co m>
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.publi c.dotnet.langua ges.csharp/<##************ **@tk2msftngp13 .phx.gbl>

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/
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Doug Thews <do*******@remo veme.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.co m>
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.c om...
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
IsBackgroun d 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.Interr upt() 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.Interrup t 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*******@remo veme.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.publi c.dotnet.langua ges.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 ThreadInterrupt edException 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******@devel op.com> wrote in message
news:u8******** ******@TK2MSFTN GP11.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.publi c.dotnet.langua ges.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19
Doug Thews <do*******@remo veme.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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #20

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

Similar topics

6
3217
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
5314
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
4456
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
1819
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
2114
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
1311
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
3058
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
12637
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
6484
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
10171
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
9991
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11595
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
10703
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
8268
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
7443
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
6231
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...
1
4965
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
2
4549
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.