PC Review


Reply
Thread Tools Rate Thread

How to cancel Async read from socket?

 
 
John Hynes
Guest
Posts: n/a
 
      25th Sep 2003
Hi,

I have a CompactFramework application that has a socket open to a server
with an asynchronous read to read notification messages from the server and
display them to the user.

I am using NetworkStream.BeginRead to start the read operation.

The problem I have is that when the user chooses to exit the application and
I call NetworkStream.Close and Socket.Shutdown, the application hangs on
exit. The debugger shows that there is one thread left running - the worker
thread for the async read which is still trying to read from the socket.

How do I cancel this read? If I call NetworkStream.EndRead from my main
thread, this just hangs waiting for the read to complete. Unfortunaterly, on
the PocketPC the application window is still visible on the screen and never
goes away.

Thanks
John


 
Reply With Quote
 
 
 
 
Joe Robe
Guest
Posts: n/a
 
      27th Sep 2003
Did you try Socket.Close, I dont know which stream you are using here ! If
you call methods on teh stream it might be possible that they dont call the
methods on the socket !

Joe Robe

"John Hynes" <(E-Mail Removed)> wrote in message
news:dKAcb.9942$(E-Mail Removed)...
> Hi,
>
> I have a CompactFramework application that has a socket open to a server
> with an asynchronous read to read notification messages from the server

and
> display them to the user.
>
> I am using NetworkStream.BeginRead to start the read operation.
>
> The problem I have is that when the user chooses to exit the application

and
> I call NetworkStream.Close and Socket.Shutdown, the application hangs on
> exit. The debugger shows that there is one thread left running - the

worker
> thread for the async read which is still trying to read from the socket.
>
> How do I cancel this read? If I call NetworkStream.EndRead from my main
> thread, this just hangs waiting for the read to complete. Unfortunaterly,

on
> the PocketPC the application window is still visible on the screen and

never
> goes away.
>
> Thanks
> John
>
>



 
Reply With Quote
 
John Hynes
Guest
Posts: n/a
 
      27th Sep 2003
Yes, shutdown code I have is:

networkStream.Close()
socket.Shutdown( SocketShutdown.Both );
socket.Close();

this thread then exits, but one thread hangs, call stack for the thread
that hangs is:

System.dll!System.Net.OSSOCK.recv
System.dll!System.Net.Sockets.Socket.ReceiveNoCheck
System.dll!System.Net.Sockets.Socket.ReceiveAsyncRequest.doRequest
System.dll!AsyncRequest.handleRequest
System.dll!System.Net.Sockets.Socket.WorkerThread.doWork
System.dll!System.Net.Sockets.Socket.WorkerThread.doWorkI
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork
mscorlib.dll!System.Threading.Timer.ring

John

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
John Hynes
Guest
Posts: n/a
 
      29th Sep 2003
Yes, shutdown code I have is:

networkStream.Close()
socket.Shutdown( SocketShutdown.Both );
socket.Close();

this thread then exits, but one thread hangs, call stack for the thread
that hangs is:

System.dll!System.Net.OSSOCK.recv
System.dll!System.Net.Sockets.Socket.ReceiveNoCheck
System.dll!System.Net.Sockets.Socket.ReceiveAsyncRequest.doRequest
System.dll!AsyncRequest.handleRequest
System.dll!System.Net.Sockets.Socket.WorkerThread.doWork
System.dll!System.Net.Sockets.Socket.WorkerThread.doWorkI
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork
mscorlib.dll!System.Threading.Timer.ring

John

"Joe Robe" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Did you try Socket.Close, I dont know which stream you are using here ! If
> you call methods on teh stream it might be possible that they dont call

the
> methods on the socket !
>
> Joe Robe
>
> "John Hynes" <(E-Mail Removed)> wrote in message
> news:dKAcb.9942$(E-Mail Removed)...
> > Hi,
> >
> > I have a CompactFramework application that has a socket open to a server
> > with an asynchronous read to read notification messages from the server

> and
> > display them to the user.
> >
> > I am using NetworkStream.BeginRead to start the read operation.
> >
> > The problem I have is that when the user chooses to exit the application

> and
> > I call NetworkStream.Close and Socket.Shutdown, the application hangs on
> > exit. The debugger shows that there is one thread left running - the

> worker
> > thread for the async read which is still trying to read from the socket.
> >
> > How do I cancel this read? If I call NetworkStream.EndRead from my main
> > thread, this just hangs waiting for the read to complete.

Unfortunaterly,
> on
> > the PocketPC the application window is still visible on the screen and

> never
> > goes away.
> >
> > Thanks
> > John
> >
> >

>
>



 
Reply With Quote
 
Lloyd Dupont
Guest
Posts: n/a
 
      27th Oct 2003
I don't know the answer to your particular question.
however, reading sample from many people (including microsoft peoples) it
appear that to do async process (like on socket) they simply create a
reader/write socket and return, and to kill it they abort the thread.
therefore I would suggest you NOT to rely on default async behavior but to
implement yours.
and to exit by aborting your thread.

"John Hynes" <(E-Mail Removed)> a écrit dans le message de
news:dKAcb.9942$(E-Mail Removed)...
> Hi,
>
> I have a CompactFramework application that has a socket open to a server
> with an asynchronous read to read notification messages from the server

and
> display them to the user.
>
> I am using NetworkStream.BeginRead to start the read operation.
>
> The problem I have is that when the user chooses to exit the application

and
> I call NetworkStream.Close and Socket.Shutdown, the application hangs on
> exit. The debugger shows that there is one thread left running - the

worker
> thread for the async read which is still trying to read from the socket.
>
> How do I cancel this read? If I call NetworkStream.EndRead from my main
> thread, this just hangs waiting for the read to complete. Unfortunaterly,

on
> the PocketPC the application window is still visible on the screen and

never
> goes away.
>
> Thanks
> John
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Async socket connection ends up in Unable to read data from.... Arno Microsoft VB .NET 5 15th Sep 2006 09:23 AM
Async socket connection ends up in Unable to read data from.... =?Utf-8?B?QXJubw==?= Microsoft VB .NET 0 6th Sep 2006 10:09 AM
Async socket EnzoA Microsoft VC .NET 1 10th May 2005 01:51 PM
Cancel asynchrous Socket Read Tod Johnson Microsoft Dot NET Compact Framework 2 18th Feb 2005 06:17 PM
Async Socket IO Pete Davis Microsoft C# .NET 2 24th Mar 2004 10:16 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:40 AM.