Solved

Thread management

Posted on 2011-03-16
3
333 Views
Last Modified: 2012-05-11
I want to create a multi threaded feature that will create up to "N" threads.  Once each thread is created I need to be able to track each thread's progress (start, processing, complete).  If all threads are currently used I need to know that so the "N" thread limit is followed.  How is this best achieved?
0
Comment
Question by:Phil5780
3 Comments
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 35145158
Check out this free e-book. It's the bible for C# threading.

http://www.albahari.com/threading/

Hope that helps.
0
 
LVL 11

Assisted Solution

by:Sudhakar Pulivarthi
Sudhakar Pulivarthi earned 200 total points
ID: 35147635
Hi,
This can be achived using .NET ThreadPool which has controlled mechanism of creation and deletion of threads as used in the application.
Set SetMaxThreads to N threads max-limit which you want  and SetMinThreads to have minimum threads always alive.

Check out the tutorial to use thread pool and mould to your requirement.
http://msdn.microsoft.com/en-us/library/0ka9477y(v=VS.85).aspx
0
 
LVL 10

Accepted Solution

by:
Nash2334 earned 300 total points
ID: 35148364
Any time you are working with multi-threaded applications the easiest way to do so will be to use the .NET BackgroundWorker.  The BackgroundWorker reports progress, supports cancellation, and has a RunWorkerCompleted event that is raised when it's done doing whatever you ask it to do.

To track the progress of N number of threads, you can simply create a List<BackgroundWorker> of background workers, then cycle through the list and check if the workers in the list are busy.

Following is one my responses regarding BWs from a previous thread:

-------------------

BackgroundWorker is the way to go.  The BW has built in management capabilities, progress reporting, exception handling and cancellation methods that make creating multi-threaded applications quite easy.   The two primary events you will want to concern yourself with are DoWork (code the background thread executes) and RunWorkerCompleted (code to execute when the thread completes).  The BW also has built in exception handling; you can check to see if any exception is thrown when the BW completes.  The RunWorkerAsync() method kicks it off.

BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);
bw.DoWork += new DoWorkEventHandler(_bw_DoWork);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bw_RunWorkerCompleted);
bw.RunWorkerAsync();
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now