Long running delegates manager

A

Alphapage

Hello,

I have a class MyWorker.
Each time I create a new instance of MyWorker, I queue it to the ThreadPool.
So, 1 MyWorker object is pooled and belongs to its thread (there can't have 2
MyWorker in 1 thread from the ThreadPool).
When MyWorker is initialized or instanciate, I use an asynchronous delegate
to execute a long running process (depending on users, the job can be
quicker). So, this async delegate will use or create a new thread from the
ThreadPool to execute.
The MyWorker thread must wait for this async delegate to complete,the async
delegate thread will free up and will return to the ThreadPool, finally the
MyWorker object is able to continue processing with the result of the async
delegate.
But that means each MyWorker use 2 threads during long running process: I
think my app is bad design and can be optimized.

To increase the scalability, I heard about a kind of long running delegates
manager. But I can't find such a tool: I only find "fire-and-forget"
delegate which will corrupt MyWorker object because it doesn't wait for the
async delegate to complete or the same process I describe and use.
I need help to build a robust code implementing those features:
- queue each MyWorker in the ThreadPool
- each time the async delegate of MyWorker is invoked, free the MyWorker
thread to let other MyWorker object or other async delegate use it
- after the async delegate is completed and the thread is free, continue and
finalize the MyWorker process. (I think the long running delegates manager
will run on the Main app thread)

Thanks in advance for your help.
 
A

Alphapage

Thanks Pete, you are right.

I can run all the MyWorker code on one thread.
But the goal is to free threads to let other faster MyWorker object execute
on other threads. I think this part of my question was not clear, sorry.

Your comment helps me and I decide to use a custom ThreadPool to process the
long running jobs instead of an async delegate. But the problem remains:
I execute MyWorker on the framework ThreadPool, if a long running job is
required, I start a thread from my custom ThreadPool. But I don't know how
to free the current framework ThreadPool thread and at the same time waiting
for the custom ThreadPool thread (ie long running job) to complete.
So, I can run more fast MyWorker.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top