473,419 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Long running delegates manager

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.
Jun 27 '08 #1
2 1605
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.
Jun 27 '08 #2
On Tue, 20 May 2008 09:28:02 -0700, Alphapage
<Al*******@discussions.microsoft.comwrote:
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.
It was clear you _think_ that's your goal. But so far, you haven't
written anything to support that idea.

You have very clearly stipulated that you have a thread that simply wants
to execute a delegate on a different thread, and while that delegate is
executing, the first thread should wait for the delegate to complete.

You have also very clearly stipulated that you want for the first thread
to give up its thread while the delegate is executing so that the thread
is available for other processing.

But: this is _exactly_ what happens if you simply call the delegate from
the first thread, without invoking it asynchronously.
Your comment helps me and I decide to use a custom ThreadPool to process
the
long running jobs instead of an async delegate.
There's nothing about the problem you've stated that argues in favor of a
custom ThreadPool.
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.
You cannot "free" a thread without simply having the code executing on
that thread return to the caller. In a thread pool, at the top of the
thread is a loop that sits and waits for delegates to execute. When a
delegate is queued to the thread pool, some thread picks up that delegate
and calls it. Until the delegate returns, that thread is committed to
executing the thread. You can't interrupt the delegate externally,
execute some other code on the same thread, and then resume the delegate.

However, what you _can_ do is simply have the first delegate call some
other delegate. This will do exactly what you're asking for: the original
delegate, queued to the thread pool, will be interrupted while the second
delegate is executing on the very same thread. When the second delegate
completes, control will return to the first delegate at which point it
will continue executing on the same thread it was always executing.

This is just how method calling works. You call a method, your own code
doesn't get control back until that method returns. All code runs on
_some_ thread, and any time some code calls a method, it's "interrupted"
until the method it called returns, yielding use of the same thread to the
method that was called.

You are REALLY overthinking this problem. It's not nearly as complicated
as you seem bent on making it. :)

Pete
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PF | last post by:
On one of our machines, all of the SQL Server 2000 components except for the main Server component (SQL Server core) itself were installed (Management tools, etc) a while ago and everything was...
2
by: Amy L. | last post by:
I have a windows service where I would like to launch a script (bat, cmd, vbs, exe, etc) from the service. This script should execute in the background and not require any input from the user...
15
by: Andrew Chalk | last post by:
I have just moved a project to my laptop and get the message "Visual Studio ..NET has detected that the specified Web server is not running ASP.NET v1.1. You will be unable to run ASP.NET Web...
29
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using...
16
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid,...
11
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our...
2
by: Pohihihi | last post by:
I have form A, B, and C For C can be called from A or B but when ever something happence in C few things on form A needs to be updated. First way was register callback on A for B and then...
14
by: Steve McLellan | last post by:
Hi, Sorry to repost, but this is becoming aggravating, and causing me a lot of wasted time. I've got a reasonably large mixed C++ project, and after a number of builds (but not a constant...
21
by: Bart C | last post by:
I've always had a problem knowing exactly how wide my integer variables were in C, and the little program below has increased my confusion. Run on 3 compilers on the same cpu (32-bit pentium),...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.