Solved

Thread Manager for C#

Posted on 2010-11-20
14
1,027 Views
Last Modified: 2012-05-10
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
Comment
Question by:TimPeer
[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
  • 7
  • 6
14 Comments
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34181721
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
 

Author Comment

by:TimPeer
ID: 34181831
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34182173
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
Technology Partners: 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!

 
LVL 8

Expert Comment

by:Volox
ID: 34182225
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
 

Author Comment

by:TimPeer
ID: 34184000
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
 

Author Comment

by:TimPeer
ID: 34185825
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34192122
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
 

Author Comment

by:TimPeer
ID: 34192331
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
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34200294
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
 

Author Comment

by:TimPeer
ID: 34200380
Will do. Thanks!
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34769646
Did you have any luck with this?
0
 

Author Comment

by:TimPeer
ID: 34773777
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
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
ID: 34774308
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
 

Author Closing Comment

by:TimPeer
ID: 34774688
I will grant the points because you remained interested in the question.
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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

724 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