473,387 Members | 3,684 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,387 software developers and data experts.

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 1466
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.Threading;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClass mtc = new MultiThreadClass();

Console.WriteLine("STARTING THREADS");
mtc.Start();

Console.WriteLine("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(6000);

Console.WriteLine("STOPPING THREADS");
mtc.Stop();

Console.WriteLine("ALL DONE");
Console.ReadLine();
}
}

class MultiThreadClass
{
private Thread _t1, _t2, _t3;
private ManualResetEvent _t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent;

private bool _continueWorking = true;

public MultiThreadClass()
{
_t1 = new Thread(new ThreadStart(Thread1DoWork));
_t2 = new Thread(new ThreadStart(Thread2DoWork));
_t3 = new Thread(new ThreadStart(Thread3DoWork));

_t1CompleteEvent = new ManualResetEvent(false);
_t2CompleteEvent = new ManualResetEvent(false);
_t3CompleteEvent = new ManualResetEvent(false);
}

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

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent};
_continueWorking = false;

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

private void Thread1DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 1 working");
Thread.Sleep(1000);
}

Console.WriteLine("Thread 1 stopped");
_t1CompleteEvent.Set();
}

private void Thread2DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 2 working");
Thread.Sleep(373);
}

Console.WriteLine("Thread 2 stopped");
_t2CompleteEvent.Set();
}

private void Thread3DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 3 working");
Thread.Sleep(555);
}

Console.WriteLine("Thread 3 stopped");
_t3CompleteEvent.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 ManualResetEvents.

"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.Threading;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClass mtc = new MultiThreadClass();

Console.WriteLine("STARTING THREADS");
mtc.Start();

Console.WriteLine("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(6000);

Console.WriteLine("STOPPING THREADS");
mtc.Stop();

Console.WriteLine("ALL DONE");
Console.ReadLine();
}
}

class MultiThreadClass
{
private Thread _t1, _t2, _t3;
private ManualResetEvent _t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent;

private bool _continueWorking = true;

public MultiThreadClass()
{
_t1 = new Thread(new ThreadStart(Thread1DoWork));
_t2 = new Thread(new ThreadStart(Thread2DoWork));
_t3 = new Thread(new ThreadStart(Thread3DoWork));

_t1CompleteEvent = new ManualResetEvent(false);
_t2CompleteEvent = new ManualResetEvent(false);
_t3CompleteEvent = new ManualResetEvent(false);
}

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

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent};
_continueWorking = false;

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

private void Thread1DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 1 working");
Thread.Sleep(1000);
}

Console.WriteLine("Thread 1 stopped");
_t1CompleteEvent.Set();
}

private void Thread2DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 2 working");
Thread.Sleep(373);
}

Console.WriteLine("Thread 2 stopped");
_t2CompleteEvent.Set();
}

private void Thread3DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 3 working");
Thread.Sleep(555);
}

Console.WriteLine("Thread 3 stopped");
_t3CompleteEvent.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*********@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.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.Threading;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MultiThreadClass mtc = new MultiThreadClass();

Console.WriteLine("STARTING THREADS");
mtc.Start();

Console.WriteLine("MAIN UI THREAD SLEEPING FOR 6 SECONDS");
Thread.Sleep(6000);

Console.WriteLine("STOPPING THREADS");
mtc.Stop();

Console.WriteLine("ALL DONE");
Console.ReadLine();
}
}

class MultiThreadClass
{
private Thread _t1, _t2, _t3;
private ManualResetEvent _t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent;

private bool _continueWorking = true;

public MultiThreadClass()
{
_t1 = new Thread(new ThreadStart(Thread1DoWork));
_t2 = new Thread(new ThreadStart(Thread2DoWork));
_t3 = new Thread(new ThreadStart(Thread3DoWork));

_t1CompleteEvent = new ManualResetEvent(false);
_t2CompleteEvent = new ManualResetEvent(false);
_t3CompleteEvent = new ManualResetEvent(false);
}

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

public void Stop()
{
WaitHandle[] handles = new WaitHandle[]{_t1CompleteEvent,
_t2CompleteEvent,
_t3CompleteEvent};
_continueWorking = false;

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

private void Thread1DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 1 working");
Thread.Sleep(1000);
}

Console.WriteLine("Thread 1 stopped");
_t1CompleteEvent.Set();
}

private void Thread2DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 2 working");
Thread.Sleep(373);
}

Console.WriteLine("Thread 2 stopped");
_t2CompleteEvent.Set();
}

private void Thread3DoWork()
{
while (_continueWorking)
{
Console.WriteLine("Thread 3 working");
Thread.Sleep(555);
}

Console.WriteLine("Thread 3 stopped");
_t3CompleteEvent.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
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...
0
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...
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...
34
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; ...
3
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...
10
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,...
6
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...
3
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...
10
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...
4
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.