如果启动的进程已打开,则在应用程序退出后套接字不会关闭 [英] Socket doesn't close after application exits if a launched process is open

查看:48
本文介绍了如果启动的进程已打开,则在应用程序退出后套接字不会关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#.net应用程序当前执行以下操作(除其他外):

My C# .net application currently does the following (among other things):

  1. 创建一个线程,该线程在特定端口上打开套接字并等待指令.
  2. 消息进入,套接字线程读取消息并引发事件.
  3. 事件处理程序调用必要的函数来解析消息并执行必要的操作,例如,启动应用程序.
  4. 指定的外部应用程序"以异步方式启动.

当我的应用程序重新启动但外部应用程序未关闭时,套接字似乎从未关闭过.然后,当我尝试再次在该端口上启动通信时,出现错误.但是,一旦我关闭外部应用程序,便可以在该端口上打开一个套接字.

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.

有什么想法吗?

推荐答案

这只是一个刺,因为我已经知道您是如何开始外部过程的.如果您要创建一个Process对象,我想您没有设置:

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:

ProcessStartInfo.UseShellExecute = true;

如果UseShellExecute设置为false,则子进程将继承父进程的打开套接字句柄.即使关闭了父应用程序,这也将使您的套接字保持打开状态.

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.

这篇关于如果启动的进程已打开,则在应用程序退出后套接字不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆