0
我想制作一个Winform应用程序,它将侦听TCP连接,然后在接收到连接时执行X.TcpListener崩溃我的Windows窗体应用程序
,我遇到的问题是,它开始监听应用进入“不响应”
这是我使用的代码时:
try
{
// set the TcpListener on port 13000
int port = 13000;
TcpListener server = new TcpListener(IPAddress.Any, port);
// Start listening for client requests
server.Start();
//Enter the listening loop
while (true)
{
MessageBox.Show(@"Waiting for a connection... ");
server.AcceptTcpClient();
MessageBox.Show(@"Connected");
// ProcessStartInfo proc = new ProcessStartInfo();
// proc.WindowStyle = ProcessWindowStyle.Hidden;
// proc.FileName = "cmd";
// proc.Arguments = "/C shutdown -f -r";
// Process.Start(proc);
}
}
catch (SocketException)
{
MessageBox.Show(@"SocketException");
}
奇怪的是,就是它作为控制台应用程序工作得很好。
任何想法我做错了什么?
accept()块。您不能在GUI事件处理程序中阻止。把它关掉。 – ThingyWotsit
表单项目不需要while循环。 while循环称为块并阻止控制台应用程序终止。表单项目在构造函数中包含一个块,并且在代码中不需要额外的块。 – jdweng
所以我可以删除,而真的,它会运行好吗? –