How to terminate async Web Service request safely?

A

awaken77

I have really annoying problem

My program polls Web Service in a loop many times, to retrieve some
"near-realtime" results. Usually once per 2-5 seconds.

To control call execution time, I use async calls with timeout.
Everything works fine, until Web Service hangs somewhere in-a-call ,
i.e. call never returns back from Web Service code.

The problem occurs when my code calls Web Service many many times ,
e.g. 3000 times and calls Abort() after each call

On CF 1.0 SP3 it throws ObjectDisposedException after program exits.
sometimes it throws 5 or 6 exceptions, after GUI shutdown.
On CF 2.0 it just hangs on exit when waiting for background threads to
be stopped.

Here is my code calling Web Service:

MyTimer timer = new MyTimer((ulong)m_proxy.timeout); // my timer class
try
{
timer.Reset();
m_handle = m_proxy.BeginRequest(paramsIn, null, null);
// Poll result until complete
while (!m_handle.IsCompleted && !timer.Check())
{
Thread.Sleep(5);
}

if (m_handle.IsCompleted)
{
m_ret = m_proxy.EndRequest(m_handle, out paramsOut);
}
else
{
((WebClientAsyncResult)m_handle).Abort();
throw new WebException("Timeout expired", null,
WebExceptionStatus.Timeout, null);
}

}
finally
{
timer = null; // not necessary
}


Here is my code to shutdown web service proxy, and cleanup resources

public void Close()
{
try
{
if(m_handle != null)
{
((WebClientAsyncResult)m_handle).Abort();
}
if(m_proxy != null)
{
m_proxy.Abort();
}
}
catch(Exception )
{
}
finally
{
m_handle = null;
m_proxy = null;
}
}


I call Close() from my polling thread, after this existing HTTP
connection should disconnect and thread should exit.
Instead, it throws exception

Instead polling for result I used Callback function, passed to
BeginRequest()
It ends up with worse result: when program terminates and WEbService
suddenly wakes up, WebService proxy internals call already disposed
callback object, with the same exception

I also tried Synchronous calls with Abort() on CF 1.0 SP3 - it ends up
with HUGE memory leak after running for 30 hours, and program ate all
system emmory
 
A

awaken77

Here is example of synchronous call with Abort(). Doesnt' work either.
The biggest drawback of this method is that all subsequent WebService
calls fail after Abort() was called,
even if WebService is OK.
Also it ends up with memory leak after execution 5000 times in a loop
if WebService is not available all the time.

try
{
m_timer = new System.Threading.Timer(new
TimerCallback(TimeoutHandler),
null,
m_proxy.timeout,
m_proxy.timeout);

m_Ret = m_proxy.Request(paramIn, out paramOut);
}
finally
{
m_timer.Dispose();
}


// timer thread
private void TimeoutHandler(Object obj)
{
m_proxy.Abort();
}
 

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