Solved

Thread management

Posted on 2011-03-16
3
337 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

751 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