My C# .net application currently does the following (among other things):
- Creates a thread which opens a socket on a specific port and waits for an instruction.
- Message comes in, socket thread reads the message and raises an event.
- An event handler calls the necessary functions to parse the message and performs the necessary action, for instance, launching an application.
- The specified external “application” launches, asynchronously.
When my application restarts, but the external application does not close, the socket doesn’t seem to ever close. Then when I try to start up communication on that port again I get an error. However, as soon as I close the external application I am able to open a socket on that port.
It seems like my program is not closing down properly. It should be killing the socket when it exits, however when the external process is running that socket never closes.
Any ideas?
This is just a stab because I have seen how you’re starting the external process. If you’re creating a Process object, my guess is you’re not setting:
If UseShellExecute is set to false, the child process will inherit open socket handles from the parent process. This will keep your socket open even if the parent application is closed.