Ryan,
| Brian, as far as the m_async_ops variable goes, I am using
| Interlocked.Increment and Interlocked.Decrement to change the value
In addition to Brian's comments: Interlocked.Decrement returns the new
decremented value, I would either use the Decrement's return value or a
SyncLock to check the variable itself.
| Jay, right now I'm calling the EndInvoke in the LookupCallback routine
which
| happens immediately when each call is finished.
I only call EndInvoke in the CallBack when no body else (such as the main
thread) cares that the async method finished or not.
| Is there any problems with
| not calling the EndInvoke immediately?
It doesn't matter when you call EndInvoke, it only matters that you call it.
If you don't call it I understand that you can leak resources.
| For instance, if the loop makes 5
| calls and the first one happens to take 30 seconds and the other 4 take on
1
| second. The first EndInvoke call will be blocking for 30 seconds even
though
| the other calls are finished.
That is the beauty of using the For Each (thinking about it, one could use a
System.Collections.Queue also). Your main process will only block for the
longest running async method, quick running async methods will return from
EndInvoke "right away".
| With the way I currently have it, does my callback function run under the
| main thread or the thread that the acync call runs under?
As Brian states the callback function runs on the thread that the ascync
call runs under.
--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"ryan" <ro*************@inval.hotmail.com> wrote in message
news:OA**************@TK2MSFTNGP15.phx.gbl...
| Thanks for all of the quick responses. I'll try out these different
methods
| over the weekend.
|
| Brian, as far as the m_async_ops variable goes, I am using
| Interlocked.Increment and Interlocked.Decrement to change the value
| m_async_ops. Won't those block another thread from reading the value or do
I
| need to also use some sort of synchronization while I read?
|
| Jay, right now I'm calling the EndInvoke in the LookupCallback routine
which
| happens immediately when each call is finished. Is there any problems with
| not calling the EndInvoke immediately? For instance, if the loop makes 5
| calls and the first one happens to take 30 seconds and the other 4 take on
1
| second. The first EndInvoke call will be blocking for 30 seconds even
though
| the other calls are finished. That reminds me of another question I have.
| With the way I currently have it, does my callback function run under the
| main thread or the thread that the acync call runs under?
|
| thanks again,
| Ryan
|
|
|
| "ryan" <ro*************@inval.hotmail.com> wrote in message
| news:eI**************@TK2MSFTNGP09.phx.gbl...
| > Hi,
| > I've omitted a large chunk of the code for clarity but the loop below is
| > how I'm calling a delegate function asynchronously. After I start the
each
| > call I'm incrementing a counter and then making the main thread sleep
| > until the counter gets back to zero. The call back function for each
call
| > decrements the counter. Is there a better way to make the thread wait
| > until all calls are complete besides using the counter? I've seen some
| > things with using an IAsyncResult but I'm not sure how to do that in my
| > case since I'm making 1 to n calls.
| > Thanks,
| > Ryan
| >
| > For iProviderIndex As Int32 = 0 To oProviders.Length - 1
| > oLookupReq = New Schema.LookupRequest
| > oLookup =
| > CType(Activator.CreateInstance(Type.GetType(oLooku pType.class_id)),
| > Schema.IPrequalLookup)
| > Del = New LookupAsyncDelegate(AddressOf oLookup.GetPrequalResponse)
| > Del.BeginInvoke(oLookupReq, AddressOf LookupCallback, Del)
| > IncrementAsyncOps()
| > Next iProviderIndex
| >
| > While m_async_ops > 0
| > Thread.Sleep(200)
| > End While
| >
|
|