• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

Thread Manager for C#

I am looking for a thread safe "thread provider/manager". The criteria for it's function might seem rudimentary but is essential for the application to run correctly.

1. Ability to create as class "component" populate it with data
2. Thread Manager should throttle the number of number of threads up to user-defined maximum thread count.
3. Thread Manager should permit communication (event notification) to the worker thread
4. Worker thread should communicate with the threadmanager component (with status information from the running worker process)

My project is written in C# VS2010 and reviewed at a few submitted projects on various sites and selected the best match though, my application seems to be colliding/ blocking which results in a "hanging state" when more than one thread executes.

Any suggestion on a thread manager either commercial or well documented? I would hope the TM is implemented as a component that can attach to my WinForm(s) and Web Services?

Many thanks in advance!

Tim
0
TimPeer
Asked:
TimPeer
  • 7
  • 6
1 Solution
 
MajorBigDealCommented:
Wow, what a good idea. I've written simple thread-creation and intra-thread communication logic a few times now and it never occurred to me to try to abstract it out.  But even something like you describe would not eliminate deadlocks, right? The threads would still have to be written to "play nice".  
0
 
TimPeerAuthor Commented:
In my application, I open a connection to a source and a target database via a worker "class", populate it with data and process rows between the source and target databases. ... a single worker thread is associated with a single table. Then I lean on the ODBC API to process the workload. Though not managed, the ODBC API  is very efficient. Adding a throttle/ "thread" manager to process many simultaneous SQL operations will replicate what I have currently via AFXBeginThread in the C++ MFC /classic C++ application.

I found an interface/manager and am reviewing it now on Koders...

http://www.koders.com/csharp/fidC0184A54281AB92AB53C279FF8EF4BEC14E08779.aspx?s=thread+manager

I would be willing to spend a few bucks on a a class library which permits the abstraction as you discussed that is "thread-safe". I think the cross thread updates can be handled with events?

I can't speak of deadlocks. Presumably (in my application) objects are not shraed as there is a one-to-one association for the thread to a data source pair  My class references the ODBC API which is unmanaged, but the thread is responsible for it's own ENV and Database handle/contexts. Unless, thread objects have structures which are inherently shared, I can't imagine a deadlock condition in my application (yet... it hangs when I run more than 1 simultaneous thread).

0
 
MajorBigDealCommented:
I am far from expert since I have only done multiple threading a few times. But logically, there must be some shared resource causing the blocking - what else could it be? It may be that the blocking is happening in the logic that you are using to communicate between threads rather than in the task logic itself.  And I'm guessing that is why you think that a thread manager might help.  Is that right?
0
Industry Leaders: 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!

 
VoloxCommented:
I've got some code that is similar to what it sounds like you are looking for but it is by no means product-ized.  But even with a well built abstraction of the thread management I can tell you that there are still plenty of easy ways to deadlock.
That's why I found it as valuable to have varying concurrency models between tasks as it was to have complicated thread management.
0
 
TimPeerAuthor Commented:
Searching about various sites I stumbled upon this solution / tutorial which seems promising.

http://www.codeproject.com/KB/threads/h5csthreading.aspx

To your questions on blocking,  I am rewriting in Managed C++ a functionality that I implementing using various lists of thread pointer lists.

-Instantiate a class
-Initialize class with data
-Launch the class as a worker thread and process against two databases.
-Inter-thread communication occurred via UDP datagrams. Events were not used in anyway.

I maintained a persistent list of pointers in the main application and used the call back function to perform clean-up, a very inelegant solution, but it worked.

In C#, having a thread manager to track the threads of execution and a mechanism to communicate with the main application seems not just more elegant, but a cleaner implementation.

My application contains a single class "object" which can be thought as a set of units of work.
The application for many years initializes the object and executes the Open() method to being processing. When the work is complete for the class, the class disposes all dependent objects, frees all database handles and sends a UDP datagram to the main application that the work is complete.  BTW: as many 40-50 worker threads could execute concurrently.

Though the process used currently isn't "broke"... a thread manager.

I am surprised there are no commercially thread management interfaces available (I couldn't locate any).
0
 
TimPeerAuthor Commented:
I reviewed QT, Intel's Threading Blocks for C++, Acrux Threading and various of solutions found on CSharp Coding web sites. Thanks to Intel's threading blocks "task" I* changed the search criteria and viola, VS2010 .Net 4.0 support for Parallelism. I think this is a perfect fit! I haven't found much information other than implementation documentation on the MS sites though.  

Can anyone create a wrapper and upload to this question which demonstrates the following:

For a control / form
 -- Create a scheduler
 -- (embed or use an object class, is called clsReplicate class in my project) as a worker thread
 -- Set a maximum threads on the scheduler
 -- schedule and execute the thread
 -- report back to the control the status of work performed.. (could be a counter or a string in demo code)
 -- safe abort to permit the thread's object class to perform clean up by calling dispose method.
 
Thanks,
Tim
0
 
MajorBigDealCommented:
Hi Tim, I do want to help but I don't think I'm in a position to learn this product and write up an example at this time.  I hope that my previous comments were of some assistance. Thanks!  
0
 
TimPeerAuthor Commented:
Thanks Major,

I picked up Andrew Troelsen's Pro C# 2010 at B&N and am applying a solution for testing now.

Many thanks!

Tim
0
 
MajorBigDealCommented:
I was wondering if you will be able to post your solution when ready.  I'm sure that would be helpful to a lot of people.
0
 
TimPeerAuthor Commented:
Will do. Thanks!
0
 
MajorBigDealCommented:
Did you have any luck with this?
0
 
TimPeerAuthor Commented:
send me your email offlist and I will send the solution that I considered. As It turned out, I reverted back to the classic callback method using thread delegates. Not as sexy TASKS, but equally effective for the work that needed to be performed.

Tim

timpeer_at_gmail.com
0
 
MajorBigDealCommented:
That makes sense, even if you abstracted the threading logic you would still need customized logic (depending on the requirements) to avoid deadlocks.  I don't think there is any way to "automatically" prevent deadlocks.

Will you go ahead and close out this question?
0
 
TimPeerAuthor Commented:
I will grant the points because you remained interested in the question.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now