Solved

Thread Manager for C#

Posted on 2010-11-20
14
996 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

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

20 Experts available now in Live!

Get 1:1 Help Now