473,664 Members | 3,066 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1627
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*******@disc ussions.microso ft.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 "interrupte d"
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
2350
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 running fine. Now I go and add/install the Server component and then Service Pack 3a. It seems that Service Manager won't start up (I get an hourglass cursor) and now I find that Enterprise Manager won't run as well. No error messages appeared...
2
279
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 (i.e. not interact with the desktop). I have been looking at System.Diagnostics.Process.Start() to launch this script. Here are my questions based on this. 1.) Is this the right namespace to be using?
15
1937
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 applications or services" Presumably there is a d/l on MSDN to rectify this, but I can't find it. Does anyone know the URL? Many thanks.
29
5798
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 the transaction because I need a way to roll it back if any errors happen during the transaction. Unfortunately all tables affected in the long running transaction are completely locked and nobody else can access any of the affected tables while...
16
2878
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, or other controls depending on the particular form. This application is setup with one MDI parent calling MDI children with the exception of one Modal form (the report viewer). When I run the application and run one of the screens that pulls...
11
5288
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 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
2
1336
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 register call back on B for C. That is a nested way of event but what I am looking is that if some kind of event manager is in the main thread that is taking care of this. It should also take care of unregistring the events for the closed forms (in...
14
2825
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 number) linking (and sometimes compiling) becomes immensely slow, and task manager shows that link.exe (or cl.exe) is barely using any processor time, but an awful lot of RAM (around 150-200MB). I'm going to keep an eye on page faults since I can't...
21
2626
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), sometimes int and long int are the same, and long long int is twice the width; or sometimes both long int and long long int are twice the width of int. This apparently all quite normal according to my c99 draft and c-faq.com. However it doesn't...
0
8437
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.