Solved

Thread management

Posted on 2011-03-16
3
332 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I get the id from URL? 19 48
Converting C# Objects into JSON file 7 39
C# Error - Add Failed 12 49
COnsume rest client 6 11
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

18 Experts available now in Live!

Get 1:1 Help Now