Cleaning up Async Web Threads

Hello,

I have a mobile application that asynchronously sends information over Web Services, when I close the application, if there was a web thread open, it throws an InvalidOperationException Exception that cannot be caught. It actually throws the error on a
Catch ex as Exception

line.

I've tried aborting all threads that I have handles on by storing the async result from the begin method in an array and iterating through the array calling abort, but it doesn't make any difference.

Any ideas?
Thanks

[536 byte] By [BadgerB] at [2007-12-16]
# 1
you'll need to place the try....catch block around the End call.
e.g if you make an async call to the GetItems Method then you should place your try catch around the EndGetItems method.

Thanks.

MarkIhimoyan at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2
Thanks for the response.

Unfortunately, it is around the end call.

Static Dim processing As Boolean

Try

TicketingInfo.online = TicketingServices.EndconnectionValid(ar)

asyncConns.Remove(ar)

If TicketingInfo.online And processing = False Then

processing = True

processPendingTransactions()

processing = False

End If

Catch ex As SoapException

TicketingInfo.online = False

processing = False

Catch ex As Exception

processing = False

End Try

BadgerB at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3
Can you provide the call stack that is obtained from the unhandled exception? Which version and service pack of .NET Compact Framework are you using?

This could happen when there are sockets still active when your process is shutting down. Are there any sockets that are active or that might be in the process of receiving data while the process is shutting down? Shutting down all active sockets to the extent that you have no threads communicating over the network should solve the problem. You could also try putting a finally after the catch to see if that helps solve the issue.

Thanks,
Sandy

SandeepPrabhakar at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
Thanks for the help,

Here's the call stack. It's happening after the program has completely shut down now. So it isn't even stopping in my code anymore. (I added code to abort all async web connections when the program shuts down).

mscorlib.dll!System.Threading.WaitHandle.CheckResultInternal(bool r = false) + 0x1c bytes
mscorlib.dll!System.Threading.ManualResetEvent.Set() + 0x11 bytes
System.dll!System.Net.HttpWebRequest.handleError(string message = "Unable to read data from the transport connection.", System.Exception inner = {"A blocking operation was interrupted by a call to WSACancelBlockingCall" }, int connUser = 1, System.Net.WebExceptionStatus status = ReceiveFailure, System.Net.Connection conn = {System.Net.Connection}) + 0x60 bytes
System.dll!System.Net.HttpWebRequest.startReceiving(System.Net.Connection connection = {System.Net.Connection}) + 0x100 bytes
System.dll!System.Net.Connection.startReceiving(System.Object ignored = <undefined value>) + 0x29 bytes
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(System.Object o = <undefined value>) + 0x36 bytes
> mscorlib.dll!System.Threading.Timer.ring() + 0x59 bytes

BadgerB at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 5
Thanks for the call stack. That was helpful. What's happening here is that the application is shutting down and there's a background thread still in the middle of some networking operation. When the app shuts down it cleans up and runs finalizers for everything and this cleans up the ManualResetEvent which later when the background thread tries to run blows up.

We have fixed this issue recently for our v2 RTM. What version of .NET compact framework are you using?

From your point of view calling abort should cleanup the networking threads. A few things to try is to wrap all this work in a class and make sure all references to the object to that class are no longer there and do a GC.Collect() and GC.WaitForPendingFinalizers() at the very end of the main function. This should make all GC references dead and run all finalizers on every object before starting to shutdown.

Could you also send a small stripped down version of your project that reproduces the problem? I will investigate better solutions to the issue.
Thanks,
Sandy

SandeepPrabhakar at 2007-9-9 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...