I have a particular requirement when I call a web service; I have to call the
web service, wait until the web service returns or my timeout expires
(whichever is sooner) and exit.
I tried to use the MSDN example to poll a web service and abort it after a
specified timeout.
Have a look at the code below:
/* Set the timeout in seconds*/
int Timeout = 20;
/* create an instance of the web service proxy*/
POLL.Service1 WS = new POLL.Service1;
/* The SleepForInterval method on the web service takes an int and sleeps
the thread by the int interval (in millisecods). */
IAsyncResult ar = WS.BeginSleepForInterval(10000), null, null);
DateTime start = DateTime.Now;
while (ar.IsCompleted == false)
{
DateTime elapsed = DateTime.Now;
TimeSpan ts = elapsed - start;
if (ts.TotalSeconds > Timeout )
{
WS.Abort();
}
}
---
What I have found is that the code snippet does not behave as expected, ie
the IsCompleted flag NEVER gets set to true, even though I know that the web
service call has completed. It just loops for ever in the while loop until
the the abort statement gets called.
Any help would be appreciated.

Signature
Regards
----------------
Shailen Sukul
MCSD MCAD
"Imagination is more important than knowledge." - Einstein
"Gravitation is not responsible for people falling in love." - Eistein
Michael Nemtsev - 20 Dec 2005 21:48 GMT
Hello Shane,
Hope this heps: http://www.yoda.arachsys.com/csharp/threads/volatility.shtml
S> I have a particular requirement when I call a web service; I have to
S> call the web service, wait until the web service returns or my
S> timeout expires (whichever is sooner) and exit.
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Shane - 20 Dec 2005 22:14 GMT
I tried locking the object as follows, alas to no avail:
static IAsyncResult ar;
static readonly object stopLock = new object();
static bool IsCompleted
{
get
{
lock (stopLock)
{
return ar.IsCompleted;
}
}
}
/* Set the timeout in seconds*/
int Timeout = 20;
/* create an instance of the web service proxy*/
POLL.Service1 WS = new POLL.Service1;
/* The SleepForInterval method on the web service takes an int and sleeps
the thread by the int interval (in millisecods). */
ar = WS.BeginSleepForInterval(10000), null, null);
DateTime start = DateTime.Now;
while (IsCompleted == false)
{
DateTime elapsed = DateTime.Now;
TimeSpan ts = elapsed - start;
if (ts.TotalSeconds > Timeout )
{
WS.Abort();
break;
}
}

Signature
Regards
----------------
Shailen Sukul
MCSD.Net MCSD MCAD
"Imagination is more important than knowledge." - Einstein
"Gravitation is not responsible for people falling in love." - Eistein
> Hello Shane,
>
[quoted text clipped - 9 lines]
> "At times one remains faithful to a cause only because its opponents do not
> cease to be insipid." (c) Friedrich Nietzsche
Shane - 20 Dec 2005 21:50 GMT
sorry need to ensure that break was called after abort.
/* Set the timeout in seconds*/
int Timeout = 20;
/* create an instance of the web service proxy*/
POLL.Service1 WS = new POLL.Service1;
/* The SleepForInterval method on the web service takes an int and sleeps
the thread by the int interval (in millisecods). */
IAsyncResult ar = WS.BeginSleepForInterval(10000), null, null);
DateTime start = DateTime.Now;
while (ar.IsCompleted == false)
{
DateTime elapsed = DateTime.Now;
TimeSpan ts = elapsed - start;
if (ts.TotalSeconds > Timeout )
{
WS.Abort();
break;
}
}

Signature
Regards
----------------
Shailen Sukul
MCSD.Net MCSD MCAD
"Imagination is more important than knowledge." - Einstein
"Gravitation is not responsible for people falling in love." - Eistein