Waiting for async webservices to finish.

J

jabailo

I am looping through a text file, and with each row, I launch a web service,
asynchronously.

Before I move on to the next step in the process, I want to make sure that
all the web services have completed.

How can I do this?

If I were to just put a Thread.Sleep with some arbitrary number ( say 5
minutes ) would the asynch web services continue to process? Or would they
be frozen while the main thread sleeps?
 
D

Dmytro Lapshyn [MVP]

Hi,

If you invoke the Web services asynchronously, you should have a reference
to each async delegate. Therefore, you can call EndInvoke on each delegate
to wait while the async process finishes. While the calling thread will be
frozen during the waiting, this will not prevent the Web service from
completing its task.
 
C

Chad Z. Hower aka Kudzu

microsoft.public.dotnet.framework.remoting
microsoft.public.dotnet.framework.webservices,
microsoft.public.dotnet.general
microsoft.public.dotnet.languages.csharp

Well at least you set a follow up. But please dont cross post. Surely you can pick on of these
as most appropriate:

http://www.hower.org/kudzu/dotnet/newsgroupguidelines.iwp




--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
 
J

jabailo

Dmytro said:
Hi,

If you invoke the Web services asynchronously, you should have a reference
to each async delegate. Therefore, you can call EndInvoke on each delegate
to wait while the async process finishes. While the calling thread will be
frozen during the waiting, this will not prevent the Web service from
completing its task.

Does calling EndInvoke make it uncessary to use the WaitForAll method of the
IAsynchResult handler?
 
J

Jay B. Harlow [MVP - Outlook]

jabailo,
The "easiest" way is to put the IAsyncResult that each of the web service
Begin* calls returns into a collection (ArrayList for example) then call the
web service End* calls for each entry in the collection.

Something like:

YourWebService ws = new YourWebService();

ArrayList results = new ArrayList();

String line = null;
while (line != null)
{
results.Add(ws.BeginSomeMethod(line, null, null));
line = reader.ReadLine();
}

foreach(IAsyncResult result in results)
{
ws.EndSomeMethod(result);
}

You may want to wrap the EndSomeMethod in a try/catch within the loop as it
may throw an exception...

Hope this helps
Jay


|
|
| I am looping through a text file, and with each row, I launch a web
service,
| asynchronously.
|
| Before I move on to the next step in the process, I want to make sure that
| all the web services have completed.
|
| How can I do this?
|
| If I were to just put a Thread.Sleep with some arbitrary number ( say 5
| minutes ) would the asynch web services continue to process? Or would
they
| be frozen while the main thread sleeps?
|
|
|
|
| --
| Texeme Textcasting Technology
| http://www.texeme.com
 
J

jabailo

I'm a little confused about what the ws.EndSomeMethod accomplishes.

For example, say I make 100 calls to a webservice asynchronously.

Say call 66, 72 and 98 run /extremely/ slow.

Ok, so after the while() loop, I have 100 threads, 97 have made a
successful call, 3 have not finished.

Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
wait, sequentially, for each method call to finish and then check the
next method call?

What if I do it this way (the way I'm doing it in my code now). Will
the code "wait" until all those threads have finished before moving on?
Or is that why I need a .WaitForAll()?:


while((s=sr.ReadLine())!=null)
{
this.as400maketable(s,tableString);
}

private void as400maketable(string line, string tablename)
{
AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
as400.BegininsertPolarData(line, tablename, delCB, null);
}

void AsyncCB(IAsyncResult ar)
{
as400.EndinsertPolarData(ar);
}
 
J

Jay B. Harlow [MVP - Outlook]

jabailo,
| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
| wait, sequentially, for each method call to finish and then check the
| next method call?
It waits sequentially for each method to finish, the foreach itself checks
for the next method.

| Say call 66, 72 and 98 run /extremely/ slow.
The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71 finished
"a long time" before 66 they return "immediately" from the ws.EndSomeMethod.
If 72 takes longer then 66, then ws.EndSomeMethod will wait for it,
otherwise it returns "immediately" also, same with 98... The net effect is
the for each only really waits as long the longest running method, plus some
very minor overhead...


| What if I do it this way (the way I'm doing it in my code now). Will
| the code "wait" until all those threads have finished before moving on?
| Or is that why I need a .WaitForAll()?:

The for each I showed is effectively a .WaitForAll. I understand that
WaitHandle.WaitAll has an implicit limitation of 64, which is an OS
limitation.

The "problem" with the call back, as you are finding, is that there is no
"obvious" way to indicate to the main thread that all the workers are
finished, where as with the for each the main thread waits for each worker
individually...

In a project where I am using ThreadPool.QueueUserWorkItem I increment a
count when I queue an item, then decrement the count when the item finishes.
You may be able to use a similar technique with the callbacks, where you
release an Manual or Auto Reset Event, to release the main thread when the
count reaches zero... However I would go with the for each over

Hope this helps
Jay

|
| I'm a little confused about what the ws.EndSomeMethod accomplishes.
|
| For example, say I make 100 calls to a webservice asynchronously.
|
| Say call 66, 72 and 98 run /extremely/ slow.
|
| Ok, so after the while() loop, I have 100 threads, 97 have made a
| successful call, 3 have not finished.
|
| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
| wait, sequentially, for each method call to finish and then check the
| next method call?
|
| What if I do it this way (the way I'm doing it in my code now). Will
| the code "wait" until all those threads have finished before moving on?
| Or is that why I need a .WaitForAll()?:
|
|
| while((s=sr.ReadLine())!=null)
| {
| this.as400maketable(s,tableString);
| }
|
| private void as400maketable(string line, string tablename)
| {
| AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
| as400.BegininsertPolarData(line, tablename, delCB, null);
| }
|
| void AsyncCB(IAsyncResult ar)
| {
| as400.EndinsertPolarData(ar);
| }
|
|
| Jay B. Harlow [MVP - Outlook] wrote:
| > jabailo,
| > The "easiest" way is to put the IAsyncResult that each of the web
service
| > Begin* calls returns into a collection (ArrayList for example) then call
the
| > web service End* calls for each entry in the collection.
| >
| > Something like:
| >
| > YourWebService ws = new YourWebService();
| >
| > ArrayList results = new ArrayList();
| >
| > String line = null;
| > while (line != null)
| > {
| > results.Add(ws.BeginSomeMethod(line, null, null));
| > line = reader.ReadLine();
| > }
| >
| > foreach(IAsyncResult result in results)
| > {
| > ws.EndSomeMethod(result);
| > }
| >
| > You may want to wrap the EndSomeMethod in a try/catch within the loop as
it
| > may throw an exception...
| >
| > Hope this helps
| > Jay
| >
| >
| > | > |
| > |
| > | I am looping through a text file, and with each row, I launch a web
| > service,
| > | asynchronously.
| > |
| > | Before I move on to the next step in the process, I want to make sure
that
| > | all the web services have completed.
| > |
| > | How can I do this?
| > |
| > | If I were to just put a Thread.Sleep with some arbitrary number ( say
5
| > | minutes ) would the asynch web services continue to process? Or would
| > they
| > | be frozen while the main thread sleeps?
| > |
| > |
| > |
| > |
| > | --
| > | Texeme Textcasting Technology
| > | http://www.texeme.com
| >
| >
 
J

jabailo

But I want them to run in parallel.

Using sync, it would be a worse case wait.

Using async, it's only as long as the longest thread.



Christopher said:
If you are waiting for it to finish, use a sync (not async) method call.
 
J

Jay B. Harlow [MVP - Outlook]

Doh!
|| Say call 66, 72 and 98 run /extremely/ slow.
| The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
finished
| "a long time" before 66 they return "immediately" from the
ws.EndSomeMethod.
That should read: The ws.EndSomeMethod will wait for 1 to 65 to finish, then
wait as long as it takes for 66 to finish. Seeing as 67 to 71...

Hope this helps
Jay

| jabailo,
|| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
|| wait, sequentially, for each method call to finish and then check the
|| next method call?
| It waits sequentially for each method to finish, the foreach itself checks
| for the next method.
|
|| Say call 66, 72 and 98 run /extremely/ slow.
| The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
finished
| "a long time" before 66 they return "immediately" from the
ws.EndSomeMethod.
| If 72 takes longer then 66, then ws.EndSomeMethod will wait for it,
| otherwise it returns "immediately" also, same with 98... The net effect is
| the for each only really waits as long the longest running method, plus
some
| very minor overhead...
|
|
|| What if I do it this way (the way I'm doing it in my code now). Will
|| the code "wait" until all those threads have finished before moving on?
|| Or is that why I need a .WaitForAll()?:
|
| The for each I showed is effectively a .WaitForAll. I understand that
| WaitHandle.WaitAll has an implicit limitation of 64, which is an OS
| limitation.
|
| The "problem" with the call back, as you are finding, is that there is no
| "obvious" way to indicate to the main thread that all the workers are
| finished, where as with the for each the main thread waits for each worker
| individually...
|
| In a project where I am using ThreadPool.QueueUserWorkItem I increment a
| count when I queue an item, then decrement the count when the item
finishes.
| You may be able to use a similar technique with the callbacks, where you
| release an Manual or Auto Reset Event, to release the main thread when the
| count reaches zero... However I would go with the for each over
|
| Hope this helps
| Jay
|
||
|| I'm a little confused about what the ws.EndSomeMethod accomplishes.
||
|| For example, say I make 100 calls to a webservice asynchronously.
||
|| Say call 66, 72 and 98 run /extremely/ slow.
||
|| Ok, so after the while() loop, I have 100 threads, 97 have made a
|| successful call, 3 have not finished.
||
|| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
|| wait, sequentially, for each method call to finish and then check the
|| next method call?
||
|| What if I do it this way (the way I'm doing it in my code now). Will
|| the code "wait" until all those threads have finished before moving on?
|| Or is that why I need a .WaitForAll()?:
||
||
|| while((s=sr.ReadLine())!=null)
|| {
|| this.as400maketable(s,tableString);
|| }
||
|| private void as400maketable(string line, string tablename)
|| {
|| AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
|| as400.BegininsertPolarData(line, tablename, delCB, null);
|| }
||
|| void AsyncCB(IAsyncResult ar)
|| {
|| as400.EndinsertPolarData(ar);
|| }
||
||
|| Jay B. Harlow [MVP - Outlook] wrote:
|| > jabailo,
|| > The "easiest" way is to put the IAsyncResult that each of the web
| service
|| > Begin* calls returns into a collection (ArrayList for example) then
call
| the
|| > web service End* calls for each entry in the collection.
|| >
|| > Something like:
|| >
|| > YourWebService ws = new YourWebService();
|| >
|| > ArrayList results = new ArrayList();
|| >
|| > String line = null;
|| > while (line != null)
|| > {
|| > results.Add(ws.BeginSomeMethod(line, null, null));
|| > line = reader.ReadLine();
|| > }
|| >
|| > foreach(IAsyncResult result in results)
|| > {
|| > ws.EndSomeMethod(result);
|| > }
|| >
|| > You may want to wrap the EndSomeMethod in a try/catch within the loop
as
| it
|| > may throw an exception...
|| >
|| > Hope this helps
|| > Jay
|| >
|| >
|| > || > |
|| > |
|| > | I am looping through a text file, and with each row, I launch a web
|| > service,
|| > | asynchronously.
|| > |
|| > | Before I move on to the next step in the process, I want to make sure
| that
|| > | all the web services have completed.
|| > |
|| > | How can I do this?
|| > |
|| > | If I were to just put a Thread.Sleep with some arbitrary number ( say
| 5
|| > | minutes ) would the asynch web services continue to process? Or
would
|| > they
|| > | be frozen while the main thread sleeps?
|| > |
|| > |
|| > |
|| > |
|| > | --
|| > | Texeme Textcasting Technology
|| > | http://www.texeme.com
|| >
|| >
|
|
 
J

jabailo

Jay said:
Doh!
|| Say call 66, 72 and 98 run /extremely/ slow.
| The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
finished
| "a long time" before 66 they return "immediately" from the
ws.EndSomeMethod.
That should read: The ws.EndSomeMethod will wait for 1 to 65 to finish,
then wait as long as it takes for 66 to finish. Seeing as 67 to 71...

How about the way I implemented it.

Will each webservice pause the main path of execution long enough to
complete?

Hope this helps
Jay

| jabailo,
|| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
|| wait, sequentially, for each method call to finish and then check the
|| next method call?
| It waits sequentially for each method to finish, the foreach itself
| checks for the next method.
|
|| Say call 66, 72 and 98 run /extremely/ slow.
| The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
finished
| "a long time" before 66 they return "immediately" from the
ws.EndSomeMethod.
| If 72 takes longer then 66, then ws.EndSomeMethod will wait for it,
| otherwise it returns "immediately" also, same with 98... The net effect
| is the for each only really waits as long the longest running method,
| plus
some
| very minor overhead...
|
|
|| What if I do it this way (the way I'm doing it in my code now). Will
|| the code "wait" until all those threads have finished before moving on?
|| Or is that why I need a .WaitForAll()?:
|
| The for each I showed is effectively a .WaitForAll. I understand that
| WaitHandle.WaitAll has an implicit limitation of 64, which is an OS
| limitation.
|
| The "problem" with the call back, as you are finding, is that there is
| no "obvious" way to indicate to the main thread that all the workers are
| finished, where as with the for each the main thread waits for each
| worker individually...
|
| In a project where I am using ThreadPool.QueueUserWorkItem I increment a
| count when I queue an item, then decrement the count when the item
finishes.
| You may be able to use a similar technique with the callbacks, where you
| release an Manual or Auto Reset Event, to release the main thread when
| the count reaches zero... However I would go with the for each over
|
| Hope this helps
| Jay
|
| ||
|| I'm a little confused about what the ws.EndSomeMethod accomplishes.
||
|| For example, say I make 100 calls to a webservice asynchronously.
||
|| Say call 66, 72 and 98 run /extremely/ slow.
||
|| Ok, so after the while() loop, I have 100 threads, 97 have made a
|| successful call, 3 have not finished.
||
|| Then I run the foreach loop...what does ws.EndSomeMethod do? Does it
|| wait, sequentially, for each method call to finish and then check the
|| next method call?
||
|| What if I do it this way (the way I'm doing it in my code now). Will
|| the code "wait" until all those threads have finished before moving on?
|| Or is that why I need a .WaitForAll()?:
||
||
|| while((s=sr.ReadLine())!=null)
|| {
|| this.as400maketable(s,tableString);
|| }
||
|| private void as400maketable(string line, string tablename)
|| {
|| AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
|| as400.BegininsertPolarData(line, tablename, delCB, null);
|| }
||
|| void AsyncCB(IAsyncResult ar)
|| {
|| as400.EndinsertPolarData(ar);
|| }
||
||
|| Jay B. Harlow [MVP - Outlook] wrote:
|| > jabailo,
|| > The "easiest" way is to put the IAsyncResult that each of the web
| service
|| > Begin* calls returns into a collection (ArrayList for example) then
call
| the
|| > web service End* calls for each entry in the collection.
|| >
|| > Something like:
|| >
|| > YourWebService ws = new YourWebService();
|| >
|| > ArrayList results = new ArrayList();
|| >
|| > String line = null;
|| > while (line != null)
|| > {
|| > results.Add(ws.BeginSomeMethod(line, null, null));
|| > line = reader.ReadLine();
|| > }
|| >
|| > foreach(IAsyncResult result in results)
|| > {
|| > ws.EndSomeMethod(result);
|| > }
|| >
|| > You may want to wrap the EndSomeMethod in a try/catch within the loop
as
| it
|| > may throw an exception...
|| >
|| > Hope this helps
|| > Jay
|| >
|| >
|| > || > |
|| > |
|| > | I am looping through a text file, and with each row, I launch a web
|| > service,
|| > | asynchronously.
|| > |
|| > | Before I move on to the next step in the process, I want to make
|| > | sure
| that
|| > | all the web services have completed.
|| > |
|| > | How can I do this?
|| > |
|| > | If I were to just put a Thread.Sleep with some arbitrary number (
|| > | say
| 5
|| > | minutes ) would the asynch web services continue to process? Or
would
|| > they
|| > | be frozen while the main thread sleeps?
|| > |
|| > |
|| > |
|| > |
|| > | --
|| > | Texeme Textcasting Technology
|| > | http://www.texeme.com
|| >
|| >
|
|
 
J

Jay B. Harlow [MVP - Outlook]

Jabailo,
| How about the way I implemented it.
|
| Will each webservice pause the main path of execution long enough to
| complete?

No. When you use a callback as you did, the main thread will immediately
continue with what it was doing.

My understanding is the callback is executed on the same thread as the
asynchronous method was executed on.

Hope this helps
Jay


| Jay B. Harlow [MVP - Outlook] wrote:
|
| > Doh!
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > | The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
| > finished
| > | "a long time" before 66 they return "immediately" from the
| > ws.EndSomeMethod.
| > That should read: The ws.EndSomeMethod will wait for 1 to 65 to finish,
| > then wait as long as it takes for 66 to finish. Seeing as 67 to 71...
|
| How about the way I implemented it.
|
| Will each webservice pause the main path of execution long enough to
| complete?
|
|
| >
| > Hope this helps
| > Jay
| >
message
| > | > | jabailo,
| > || Then I run the foreach loop...what does ws.EndSomeMethod do? Does
it
| > || wait, sequentially, for each method call to finish and then check the
| > || next method call?
| > | It waits sequentially for each method to finish, the foreach itself
| > | checks for the next method.
| > |
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > | The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
| > finished
| > | "a long time" before 66 they return "immediately" from the
| > ws.EndSomeMethod.
| > | If 72 takes longer then 66, then ws.EndSomeMethod will wait for it,
| > | otherwise it returns "immediately" also, same with 98... The net
effect
| > | is the for each only really waits as long the longest running method,
| > | plus
| > some
| > | very minor overhead...
| > |
| > |
| > || What if I do it this way (the way I'm doing it in my code now).
Will
| > || the code "wait" until all those threads have finished before moving
on?
| > || Or is that why I need a .WaitForAll()?:
| > |
| > | The for each I showed is effectively a .WaitForAll. I understand that
| > | WaitHandle.WaitAll has an implicit limitation of 64, which is an OS
| > | limitation.
| > |
| > | The "problem" with the call back, as you are finding, is that there is
| > | no "obvious" way to indicate to the main thread that all the workers
are
| > | finished, where as with the for each the main thread waits for each
| > | worker individually...
| > |
| > | In a project where I am using ThreadPool.QueueUserWorkItem I increment
a
| > | count when I queue an item, then decrement the count when the item
| > finishes.
| > | You may be able to use a similar technique with the callbacks, where
you
| > | release an Manual or Auto Reset Event, to release the main thread when
| > | the count reaches zero... However I would go with the for each over
| > |
| > | Hope this helps
| > | Jay
| > |
| > | | > ||
| > || I'm a little confused about what the ws.EndSomeMethod accomplishes.
| > ||
| > || For example, say I make 100 calls to a webservice asynchronously.
| > ||
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > ||
| > || Ok, so after the while() loop, I have 100 threads, 97 have made a
| > || successful call, 3 have not finished.
| > ||
| > || Then I run the foreach loop...what does ws.EndSomeMethod do? Does
it
| > || wait, sequentially, for each method call to finish and then check the
| > || next method call?
| > ||
| > || What if I do it this way (the way I'm doing it in my code now).
Will
| > || the code "wait" until all those threads have finished before moving
on?
| > || Or is that why I need a .WaitForAll()?:
| > ||
| > ||
| > || while((s=sr.ReadLine())!=null)
| > || {
| > || this.as400maketable(s,tableString);
| > || }
| > ||
| > || private void as400maketable(string line, string tablename)
| > || {
| > || AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
| > || as400.BegininsertPolarData(line, tablename, delCB, null);
| > || }
| > ||
| > || void AsyncCB(IAsyncResult ar)
| > || {
| > || as400.EndinsertPolarData(ar);
| > || }
| > ||
| > ||
| > || Jay B. Harlow [MVP - Outlook] wrote:
| > || > jabailo,
| > || > The "easiest" way is to put the IAsyncResult that each of the web
| > | service
| > || > Begin* calls returns into a collection (ArrayList for example) then
| > call
| > | the
| > || > web service End* calls for each entry in the collection.
| > || >
| > || > Something like:
| > || >
| > || > YourWebService ws = new YourWebService();
| > || >
| > || > ArrayList results = new ArrayList();
| > || >
| > || > String line = null;
| > || > while (line != null)
| > || > {
| > || > results.Add(ws.BeginSomeMethod(line, null, null));
| > || > line = reader.ReadLine();
| > || > }
| > || >
| > || > foreach(IAsyncResult result in results)
| > || > {
| > || > ws.EndSomeMethod(result);
| > || > }
| > || >
| > || > You may want to wrap the EndSomeMethod in a try/catch within the
loop
| > as
| > | it
| > || > may throw an exception...
| > || >
| > || > Hope this helps
| > || > Jay
| > || >
| > || >
| > || > | > || > |
| > || > |
| > || > | I am looping through a text file, and with each row, I launch a
web
| > || > service,
| > || > | asynchronously.
| > || > |
| > || > | Before I move on to the next step in the process, I want to make
| > || > | sure
| > | that
| > || > | all the web services have completed.
| > || > |
| > || > | How can I do this?
| > || > |
| > || > | If I were to just put a Thread.Sleep with some arbitrary number (
| > || > | say
| > | 5
| > || > | minutes ) would the asynch web services continue to process? Or
| > would
| > || > they
| > || > | be frozen while the main thread sleeps?
| > || > |
| > || > |
| > || > |
| > || > |
| > || > | --
| > || > | Texeme Textcasting Technology
| > || > | http://www.texeme.com
| > || >
| > || >
| > |
| > |
|
| --
| Texeme Textcasting Technology
| http://www.texeme.com
 
J

jabailo

Jay said:
Jabailo,
| How about the way I implemented it.
|
| Will each webservice pause the main path of execution long enough to
| complete?

No. When you use a callback as you did, the main thread will immediately
continue with what it was doing.

My understanding is the callback is executed on the same thread as the
asynchronous method was executed on.

I could probably put the callback responses into a global static
ArrayList then and use your foreach method there.

I wonder if the WaitAll can take an ArrayList as well as an array of
AsyncCallback...

Hope this helps
Jay


| Jay B. Harlow [MVP - Outlook] wrote:
|
| > Doh!
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > | The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
| > finished
| > | "a long time" before 66 they return "immediately" from the
| > ws.EndSomeMethod.
| > That should read: The ws.EndSomeMethod will wait for 1 to 65 to finish,
| > then wait as long as it takes for 66 to finish. Seeing as 67 to 71...
|
| How about the way I implemented it.
|
| Will each webservice pause the main path of execution long enough to
| complete?
|
|
| >
| > Hope this helps
| > Jay
| >
message
| > | > | jabailo,
| > || Then I run the foreach loop...what does ws.EndSomeMethod do? Does
it
| > || wait, sequentially, for each method call to finish and then check the
| > || next method call?
| > | It waits sequentially for each method to finish, the foreach itself
| > | checks for the next method.
| > |
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > | The ws.EndSomeMethod will wait for 66 to finish. Seeing as 67 to 71
| > finished
| > | "a long time" before 66 they return "immediately" from the
| > ws.EndSomeMethod.
| > | If 72 takes longer then 66, then ws.EndSomeMethod will wait for it,
| > | otherwise it returns "immediately" also, same with 98... The net
effect
| > | is the for each only really waits as long the longest running method,
| > | plus
| > some
| > | very minor overhead...
| > |
| > |
| > || What if I do it this way (the way I'm doing it in my code now).
Will
| > || the code "wait" until all those threads have finished before moving
on?
| > || Or is that why I need a .WaitForAll()?:
| > |
| > | The for each I showed is effectively a .WaitForAll. I understand that
| > | WaitHandle.WaitAll has an implicit limitation of 64, which is an OS
| > | limitation.
| > |
| > | The "problem" with the call back, as you are finding, is that there is
| > | no "obvious" way to indicate to the main thread that all the workers
are
| > | finished, where as with the for each the main thread waits for each
| > | worker individually...
| > |
| > | In a project where I am using ThreadPool.QueueUserWorkItem I increment
a
| > | count when I queue an item, then decrement the count when the item
| > finishes.
| > | You may be able to use a similar technique with the callbacks, where
you
| > | release an Manual or Auto Reset Event, to release the main thread when
| > | the count reaches zero... However I would go with the for each over
| > |
| > | Hope this helps
| > | Jay
| > |
| > | | > ||
| > || I'm a little confused about what the ws.EndSomeMethod accomplishes.
| > ||
| > || For example, say I make 100 calls to a webservice asynchronously.
| > ||
| > || Say call 66, 72 and 98 run /extremely/ slow.
| > ||
| > || Ok, so after the while() loop, I have 100 threads, 97 have made a
| > || successful call, 3 have not finished.
| > ||
| > || Then I run the foreach loop...what does ws.EndSomeMethod do? Does
it
| > || wait, sequentially, for each method call to finish and then check the
| > || next method call?
| > ||
| > || What if I do it this way (the way I'm doing it in my code now).
Will
| > || the code "wait" until all those threads have finished before moving
on?
| > || Or is that why I need a .WaitForAll()?:
| > ||
| > ||
| > || while((s=sr.ReadLine())!=null)
| > || {
| > || this.as400maketable(s,tableString);
| > || }
| > ||
| > || private void as400maketable(string line, string tablename)
| > || {
| > || AsyncCallback delCB = new AsyncCallback(this.AsyncCB);
| > || as400.BegininsertPolarData(line, tablename, delCB, null);
| > || }
| > ||
| > || void AsyncCB(IAsyncResult ar)
| > || {
| > || as400.EndinsertPolarData(ar);
| > || }
| > ||
| > ||
| > || Jay B. Harlow [MVP - Outlook] wrote:
| > || > jabailo,
| > || > The "easiest" way is to put the IAsyncResult that each of the web
| > | service
| > || > Begin* calls returns into a collection (ArrayList for example) then
| > call
| > | the
| > || > web service End* calls for each entry in the collection.
| > || >
| > || > Something like:
| > || >
| > || > YourWebService ws = new YourWebService();
| > || >
| > || > ArrayList results = new ArrayList();
| > || >
| > || > String line = null;
| > || > while (line != null)
| > || > {
| > || > results.Add(ws.BeginSomeMethod(line, null, null));
| > || > line = reader.ReadLine();
| > || > }
| > || >
| > || > foreach(IAsyncResult result in results)
| > || > {
| > || > ws.EndSomeMethod(result);
| > || > }
| > || >
| > || > You may want to wrap the EndSomeMethod in a try/catch within the
loop
| > as
| > | it
| > || > may throw an exception...
| > || >
| > || > Hope this helps
| > || > Jay
| > || >
| > || >
| > || > | > || > |
| > || > |
| > || > | I am looping through a text file, and with each row, I launch a
web
| > || > service,
| > || > | asynchronously.
| > || > |
| > || > | Before I move on to the next step in the process, I want to make
| > || > | sure
| > | that
| > || > | all the web services have completed.
| > || > |
| > || > | How can I do this?
| > || > |
| > || > | If I were to just put a Thread.Sleep with some arbitrary number (
| > || > | say
| > | 5
| > || > | minutes ) would the asynch web services continue to process? Or
| > would
| > || > they
| > || > | be frozen while the main thread sleeps?
| > || > |
| > || > |
| > || > |
| > || > |
| > || > | --
| > || > | Texeme Textcasting Technology
| > || > | http://www.texeme.com
| > || >
| > || >
| > |
| > |
|
| --
| Texeme Textcasting Technology
| http://www.texeme.com
 
R

Richard L Rosenheim

The Thread.Sleep only effects the current thread. The web service itself is
running in a completely different process, if not physically on a different
machine. So sleeping the thread shouldn't have any effect on the executing
of the web service itself.

You can also use the IsCompleted property to determine if the web service
has finished.

Hope this helps,

Richard Rosenheim
 
G

Guest

when u got the IAsyncResult
call this :

ar.AsyncWaitHandle.WaitOne();
(assume u IAsncResult instance is ar)

it will wait the asnyc call to complete

optioanly , u can call
ar.IsCompleted() to decide
 

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