Can't exit app after BeginReceive

M

Mike

I've seen a bit of traffic in this group about similar problems but am
not quite sure if this is the same. I have an app that needs to read
asynchronous data over a socket forever, handling it as it comes in.
The app needs to continue to perform other tasks based on user input.

So, I create a socket, connect it to my serving app (running on the
same device) and call BeginRecieve. All is working as expected except
that when I call Application.Exit(), the application does not exit.
I've tried calling Shutdown() on the socket, but this did not help.

Thanks,
Mike
 
P

Paul G. Tobey [eMVP]

There's a thread being started by that call and the thread isn't exiting. I
don't know what would break it out there, but I'd try calling socket.Close()
before anything else. For better control over what's going on, use your own
thread with blocking calls rather than BeginXXX(), which creates the thread
behind your back.

Paul T.
 
R

Robert Jordan

Mike said:
I've seen a bit of traffic in this group about similar problems but am
not quite sure if this is the same. I have an app that needs to read
asynchronous data over a socket forever, handling it as it comes in.
The app needs to continue to perform other tasks based on user input.

So, I create a socket, connect it to my serving app (running on the
same device) and call BeginRecieve. All is working as expected except
that when I call Application.Exit(), the application does not exit.
I've tried calling Shutdown() on the socket, but this did not help.

You may try ExitProcess:

[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern void ExitProcess(uint uExitCode);

Rob
 
P

Paul G. Tobey [eMVP]

I don't think that you want to do that, even if it works. When you have a
problem that is caused by you leaving some resources, a thread in this case,
allocated, the right fix is to deallocate them, not to find a bigger hammer
to exit the process with.

Paul T.

Robert Jordan said:
Mike said:
I've seen a bit of traffic in this group about similar problems but am
not quite sure if this is the same. I have an app that needs to read
asynchronous data over a socket forever, handling it as it comes in.
The app needs to continue to perform other tasks based on user input.

So, I create a socket, connect it to my serving app (running on the
same device) and call BeginRecieve. All is working as expected except
that when I call Application.Exit(), the application does not exit.
I've tried calling Shutdown() on the socket, but this did not help.

You may try ExitProcess:

[System.Runtime.InteropServices.DllImport("coredll.dll")]
public static extern void ExitProcess(uint uExitCode);

Rob
 
M

Mike

I've tried calling Close after calling Shutdown, as the documentation
says to do, but then I get a System.ObjectDisposedException. I'll give
the direct threading method a try.
 
M

Mike

I had tried all cominations of Shutdown, Close, and EndReceive that I
could think of. I hate to say it (or love to say it) but...

I switched back to my PC from the laptop I was working on, and now
everything is fine (without calling Close, Shutdown, or anything). So
I'll have to chalk this up to user error or a bad insall on my laptop
(using VS2005 beta 2 in both cases).

Thanks for the help.
 

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