473,397 Members | 1,949 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,397 software developers and data experts.

BeginInvoke async callback

Is The following Code valid it is like a mini thread pool(a friend at
work wrote it)?
I have cut down the code a bit but kept the essential parts.
The main part that I am would like explainedis when the async callback
gets called and the the line
m_WaitCallback.EndInvoke(AsyncResult);
How does it know which method has ended. i.e. does the AsyncResult
identify it?? Is it accepable to have one member variable used for all
of the begininvoke calls.

Thanks for any help,
Nick

public class SerialisedWorkerQueue
{
private WaitCallback m_WaitCallback;

private Queue m_Queue;
private int m_AvailableThreads;
private int m_ThreadsInUse;

public SerialisedWorkerQueue()
{
m_WaitCallback=new WaitCallback
(PerformWork_Internal);
m_Queue=new Queue();
m_AvailableThreads=5;
m_ThreadsInUse=0;
}

public void Add(object WorkerItem)
{
m_Queue.Enqueue(WorkerItem);
if (m_ThreadsInUse<m_AvailableThreads)
SpawnWork(null);
}

private void SpawnWork(IAsyncResult AsyncResult)
{
lock(this)
{
if(AsyncResult!=null)
{
m_WaitCallback.EndInvoke(AsyncResult);
m_ThreadsInUse--;
}

// oWorker item will be null if the queue was empty
if(m_Queue.Count>0)
{
m_ThreadsInUse++;

//Gets the next piece of work to perform
object oWorkerItem = m_Queue.Dequeue();
//Hooks up the callback to this method.
AsyncCallback oAsyncCallback = new AsyncCallback(this.SpawnWork);
//Invokes the work on the threadpool
m_WaitCallback.BeginInvoke(oWorkerItem,oAsyncCallb ack,null);
}
}
}

private void PerformWork_Internal(object WorkerItem)
{
//Do Work...
}
}

Nov 17 '05 #1
7 4506
> m_WaitCallback.EndInvoke(AsyncResult);
How does it know which method has ended. i.e. does the AsyncResult
identify it??
Note that SpawnWork() is passed as information to the BeginInvoke() call.
That means it will be called as an event handler when the asynchronous
method is done. The parameter AsyncResult indeed identifies the method
which has ended.
Is it accepable to have one member variable used for all
of the begininvoke calls.

I think (not sure) that WaitCallback is just a delegate pointing to
PerformWork_Internal. It would be OK to reuse that information, so one
member variable is ok.

Greetings,
Wessel
Nov 17 '05 #2
Nick,

This is what the last parameter (or second to last parameter) on the
call to BeginInvoke is for. It allows you to pass a state to the method
which you can retrieve from the IAsyncResult implementation passed to your
callback which allows you to identify which method you called, as well as
any other state information you would need.

You could use one member variable for all of the BeginInvoke calls, but
depending on the implementation, it might or might not be allowed (you might
have to match it up with the appropriate instance, or not).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ni*********@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Is The following Code valid it is like a mini thread pool(a friend at
work wrote it)?
I have cut down the code a bit but kept the essential parts.
The main part that I am would like explainedis when the async callback
gets called and the the line
m_WaitCallback.EndInvoke(AsyncResult);
How does it know which method has ended. i.e. does the AsyncResult
identify it?? Is it accepable to have one member variable used for all
of the begininvoke calls.

Thanks for any help,
Nick

public class SerialisedWorkerQueue
{
private WaitCallback m_WaitCallback;

private Queue m_Queue;
private int m_AvailableThreads;
private int m_ThreadsInUse;

public SerialisedWorkerQueue()
{
m_WaitCallback=new WaitCallback
(PerformWork_Internal);
m_Queue=new Queue();
m_AvailableThreads=5;
m_ThreadsInUse=0;
}

public void Add(object WorkerItem)
{
m_Queue.Enqueue(WorkerItem);
if (m_ThreadsInUse<m_AvailableThreads)
SpawnWork(null);
}

private void SpawnWork(IAsyncResult AsyncResult)
{
lock(this)
{
if(AsyncResult!=null)
{
m_WaitCallback.EndInvoke(AsyncResult);
m_ThreadsInUse--;
}

// oWorker item will be null if the queue was empty
if(m_Queue.Count>0)
{
m_ThreadsInUse++;

//Gets the next piece of work to perform
object oWorkerItem = m_Queue.Dequeue();
//Hooks up the callback to this method.
AsyncCallback oAsyncCallback = new AsyncCallback(this.SpawnWork);
//Invokes the work on the threadpool
m_WaitCallback.BeginInvoke(oWorkerItem,oAsyncCallb ack,null);
}
}
}

private void PerformWork_Internal(object WorkerItem)
{
//Do Work...
}
}

Nov 17 '05 #3
Thanks for your replys, Just one more question then would I be able to
do the following:

SerialisedWorkerQueue swq = new SerialisedWorkerQueue()
swq.Add(null);
swq.Add(null);
swq.Add(null);

and expect it to *always* work OK??
Thanks again

Nov 17 '05 #4
Nick,

No, I wouldn't, because you don't have anything in the Add method that
is serializing access to the collection.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ni*********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Thanks for your replys, Just one more question then would I be able to
do the following:

SerialisedWorkerQueue swq = new SerialisedWorkerQueue()
swq.Add(null);
swq.Add(null);
swq.Add(null);

and expect it to *always* work OK??
Thanks again

Nov 17 '05 #5
Missed out the lock. so this would make the above question work??

public void Add(object WorkerItem)
{
lock(this)
{
m_Queue.Enqueue(WorkerItem);
if (m_ThreadsInUse<m_AvailableThr*eads)
SpawnWork(null);
}
}

Nov 17 '05 #6
I would think that the lock needs to be in the SpawnWork method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ni*********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Missed out the lock. so this would make the above question work??

public void Add(object WorkerItem)
{
lock(this)
{
m_Queue.Enqueue(WorkerItem);
if (m_ThreadsInUse<m_AvailableThr*eads)
SpawnWork(null);
}
}
Nov 17 '05 #7
I would think that the lock needs to be in the SpawnWork method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ni*********@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Missed out the lock. so this would make the above question work??

public void Add(object WorkerItem)
{
lock(this)
{
m_Queue.Enqueue(WorkerItem);
if (m_ThreadsInUse<m_AvailableThr*eads)
SpawnWork(null);
}
}
Nov 17 '05 #8

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

Similar topics

1
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these...
1
by: Gomaw Beoyr | last post by:
When the BeginInvoke method of a delegate is called, the execution of the delegate is in another thread. (I'm not sure if an entirely new thread is created, or if a "sleeping" thread is used.) ...
9
by: David Sworder | last post by:
Hi, I have a form that displays data (is that vague enough for you?). The data comes in on a thread-pool thread. Since the thread pool thread is not the same as the UI thread, the callback...
2
by: Jet Leung | last post by:
Hi All, There is a problem about asynchronism process.If I run the method by BeginInvoke() and how can I know when the method is finish? And maybe I can say when I can use EndInvoke()?
2
by: Nick Palmius | last post by:
Hello, I am experimenting with async callbacks using BeginInvoke and EndInvoke, and although my code which I have shown below works, when the program stops at the end (on the ReadLine()), there...
2
by: Stampede | last post by:
Hi guys 'n' girls, I want to use callback methods when using BeginInvoke on some events. So far no problem, but know I thought about what could happen (if I'm not completly wrong). Lets say an...
6
by: Shak | last post by:
Hi all, Three questions really: 1) The async call to the networkstream's endread() (or even endxxx() in general) blocks. Async calls are made on the threadpool - aren't we advised not to...
2
by: Flack | last post by:
Hello, If I understand BeginInvoke correctly, when it is called your delegate is run on a thread pool thread. Now, if you supplied a callback delegate, that too is called on the same thread...
10
by: Frankie | last post by:
It appears that System.Random would provide an acceptable means through which to generate a unique value used to identify multiple/concurrent asynchronous tasks. The usage of the value under...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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,...
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.