473,788 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threads.

EAI
Hello All,

How to abort or make sure the child threads are aborted before aborting the
parent thread?

Thanks
Nov 17 '05 #1
3 1486
Hi EAI,
you can use Revents to indicate that all worker threads have finished
working before stopping the main thread. I would not recommend you aborting
the tread as that can lead to unforseen problems and indeterminate state,
rather if you can use something like a boolean to stop the threads.

Here is an example of three worker threads being run by the main UI thread,
the threee worker threads are all stopped before the main UI thread can
continue:

using System;
using System.Threadin g;

namespace ConsoleApplicat ion2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClas s mtc = new MultiThreadClas s();

Console.WriteLi ne("STARTING THREADS");
mtc.Start();

Console.WriteLi ne("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(60 00);

Console.WriteLi ne("STOPPING THREADS");
mtc.Stop();

Console.WriteLi ne("ALL DONE");
Console.ReadLin e();
}
}

class MultiThreadClas s
{
private Thread _t1, _t2, _t3;
private ManualResetEven t _t1CompleteEven t,
_t2CompleteEven t,
_t3CompleteEven t;

private bool _continueWorkin g = true;

public MultiThreadClas s()
{
_t1 = new Thread(new ThreadStart(Thr ead1DoWork));
_t2 = new Thread(new ThreadStart(Thr ead2DoWork));
_t3 = new Thread(new ThreadStart(Thr ead3DoWork));

_t1CompleteEven t = new ManualResetEven t(false);
_t2CompleteEven t = new ManualResetEven t(false);
_t3CompleteEven t = new ManualResetEven t(false);
}

public void Start()
{
_t1.Start();
_t2.Start();
_t3.Start();
}

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEve nt,
_t2CompleteEven t,
_t3CompleteEven t};
_continueWorkin g = false;

//will wait here until all threads have finished
WaitHandle.Wait All(handles);
}

private void Thread1DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 1 working");
Thread.Sleep(10 00);
}

Console.WriteLi ne("Thread 1 stopped");
_t1CompleteEven t.Set();
}

private void Thread2DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 2 working");
Thread.Sleep(37 3);
}

Console.WriteLi ne("Thread 2 stopped");
_t2CompleteEven t.Set();
}

private void Thread3DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 3 working");
Thread.Sleep(55 5);
}

Console.WriteLi ne("Thread 3 stopped");
_t3CompleteEven t.Set();
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org


"EAI" wrote:
Hello All,

How to abort or make sure the child threads are aborted before aborting the
parent thread?

Thanks

Nov 17 '05 #2
Sorry typo, I didn't mean Revents, I just meant threading based events in
general like ManualResetEven ts.

"Mark R. Dawson" wrote:
Hi EAI,
you can use Revents to indicate that all worker threads have finished
working before stopping the main thread. I would not recommend you aborting
the tread as that can lead to unforseen problems and indeterminate state,
rather if you can use something like a boolean to stop the threads.

Here is an example of three worker threads being run by the main UI thread,
the threee worker threads are all stopped before the main UI thread can
continue:

using System;
using System.Threadin g;

namespace ConsoleApplicat ion2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClas s mtc = new MultiThreadClas s();

Console.WriteLi ne("STARTING THREADS");
mtc.Start();

Console.WriteLi ne("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(60 00);

Console.WriteLi ne("STOPPING THREADS");
mtc.Stop();

Console.WriteLi ne("ALL DONE");
Console.ReadLin e();
}
}

class MultiThreadClas s
{
private Thread _t1, _t2, _t3;
private ManualResetEven t _t1CompleteEven t,
_t2CompleteEven t,
_t3CompleteEven t;

private bool _continueWorkin g = true;

public MultiThreadClas s()
{
_t1 = new Thread(new ThreadStart(Thr ead1DoWork));
_t2 = new Thread(new ThreadStart(Thr ead2DoWork));
_t3 = new Thread(new ThreadStart(Thr ead3DoWork));

_t1CompleteEven t = new ManualResetEven t(false);
_t2CompleteEven t = new ManualResetEven t(false);
_t3CompleteEven t = new ManualResetEven t(false);
}

public void Start()
{
_t1.Start();
_t2.Start();
_t3.Start();
}

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEve nt,
_t2CompleteEven t,
_t3CompleteEven t};
_continueWorkin g = false;

//will wait here until all threads have finished
WaitHandle.Wait All(handles);
}

private void Thread1DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 1 working");
Thread.Sleep(10 00);
}

Console.WriteLi ne("Thread 1 stopped");
_t1CompleteEven t.Set();
}

private void Thread2DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 2 working");
Thread.Sleep(37 3);
}

Console.WriteLi ne("Thread 2 stopped");
_t2CompleteEven t.Set();
}

private void Thread3DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 3 working");
Thread.Sleep(55 5);
}

Console.WriteLi ne("Thread 3 stopped");
_t3CompleteEven t.Set();
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org


"EAI" wrote:
Hello All,

How to abort or make sure the child threads are aborted before aborting the
parent thread?

Thanks

Nov 17 '05 #3
EAI
Hi Mark,

I dont want to wait for the thread to end because child threads are
schedulers which keep on running. Once I start them they are supposed to run
till I terminate them.

Thanks

"Mark R. Dawson" <Ma*********@di scussions.micro soft.com> wrote in message
news:62******** *************** ***********@mic rosoft.com...
Hi EAI,
you can use Revents to indicate that all worker threads have finished
working before stopping the main thread. I would not recommend you
aborting
the tread as that can lead to unforseen problems and indeterminate state,
rather if you can use something like a boolean to stop the threads.

Here is an example of three worker threads being run by the main UI
thread,
the threee worker threads are all stopped before the main UI thread can
continue:

using System;
using System.Threadin g;

namespace ConsoleApplicat ion2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClas s mtc = new MultiThreadClas s();

Console.WriteLi ne("STARTING THREADS");
mtc.Start();

Console.WriteLi ne("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(60 00);

Console.WriteLi ne("STOPPING THREADS");
mtc.Stop();

Console.WriteLi ne("ALL DONE");
Console.ReadLin e();
}
}

class MultiThreadClas s
{
private Thread _t1, _t2, _t3;
private ManualResetEven t _t1CompleteEven t,
_t2CompleteEven t,
_t3CompleteEven t;

private bool _continueWorkin g = true;

public MultiThreadClas s()
{
_t1 = new Thread(new ThreadStart(Thr ead1DoWork));
_t2 = new Thread(new ThreadStart(Thr ead2DoWork));
_t3 = new Thread(new ThreadStart(Thr ead3DoWork));

_t1CompleteEven t = new ManualResetEven t(false);
_t2CompleteEven t = new ManualResetEven t(false);
_t3CompleteEven t = new ManualResetEven t(false);
}

public void Start()
{
_t1.Start();
_t2.Start();
_t3.Start();
}

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEve nt,
_t2CompleteEven t,
_t3CompleteEven t};
_continueWorkin g = false;

//will wait here until all threads have finished
WaitHandle.Wait All(handles);
}

private void Thread1DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 1 working");
Thread.Sleep(10 00);
}

Console.WriteLi ne("Thread 1 stopped");
_t1CompleteEven t.Set();
}

private void Thread2DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 2 working");
Thread.Sleep(37 3);
}

Console.WriteLi ne("Thread 2 stopped");
_t2CompleteEven t.Set();
}

private void Thread3DoWork()
{
while (_continueWorki ng)
{
Console.WriteLi ne("Thread 3 working");
Thread.Sleep(55 5);
}

Console.WriteLi ne("Thread 3 stopped");
_t3CompleteEven t.Set();
}
}
}
Hope that helps
Mark R Dawson
http://www.markdawson.org


"EAI" wrote:
Hello All,

How to abort or make sure the child threads are aborted before aborting
the
parent thread?

Thanks

Nov 17 '05 #4

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

Similar topics

3
5403
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import thread, sys
0
2011
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux distributions using the Native Posix Threading Layer (NPTL), this isn't entirely true anymore and is AFAIK unsupported (using a pid to signal/identify threads). I removed it from my config.sh script and ran Configure -der. make test runs just fine. ...
6
3206
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...
34
10813
by: Kovan Akrei | last post by:
Hi, I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program: using System; using System.Threading; using System.Collections; public class A {
3
22177
by: bygandhi | last post by:
Hi - I am writing a service which will check a process and its threads for their state ( alive or dead ). The process has 5 .net managed threads created using thread.start and each have been assigned a name. As in .net there is no way we can get the managed threads, I am thinking of making a win32 call and check their status.
10
1682
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes, OS execute (All have same priority) Thread#1 may be other threads, Thread#2 may be other threads, Thread#3 may be other threads,
6
2528
by: RahimAsif | last post by:
Hi guys, I would like some advice on thread programming using C#. I am writing an application that communicates with a panel over ethernet, collects data and writes it to a file. The way the data is collected is that we have different schedules (so one set of data is collected say every second, another set of data might be collected every 30 seconds, and so on).
3
5974
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
10
1762
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that T2, T4 and T5 are already running. Thanks, Darian
4
2259
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should start five threads and process five files at a time. Is this a good idea? I picked the number five at random... I was thinking that I might check the number of processors and start a multiple of that, but then I remembered KISS and it seemed that...
0
9656
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
10364
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
10172
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...
0
9967
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
7517
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
6750
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();...
1
4069
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
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.