Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 启动服务器会使我的应用程序挂起_C# - Fatal编程技术网

C# 启动服务器会使我的应用程序挂起

C# 启动服务器会使我的应用程序挂起,c#,C#,所以在过去的4天里,我用TCPListener和TCPClient进行了测试。我已经很容易地创建了一个独立的服务器/侦听器和一个独立的发送者/客户端。现在,我试图在一个应用程序中完成这一切,但当我尝试启动服务器时,整个应用程序将挂起。我不明白为什么,因为它几乎和我在不同应用程序中使用的代码相同 我已经尝试了它的多种变体,并观看了多种教程,但由于它实际上是我已经使用过的相同代码,我不知道发生了什么 当我单击启动按钮触发StartButton\u click方法时,问题就会出现 使用系统; 使用Sy

所以在过去的4天里,我用TCPListener和TCPClient进行了测试。我已经很容易地创建了一个独立的服务器/侦听器和一个独立的发送者/客户端。现在,我试图在一个应用程序中完成这一切,但当我尝试启动服务器时,整个应用程序将挂起。我不明白为什么,因为它几乎和我在不同应用程序中使用的代码相同

我已经尝试了它的多种变体,并观看了多种教程,但由于它实际上是我已经使用过的相同代码,我不知道发生了什么

当我单击启动按钮触发StartButton\u click方法时,问题就会出现

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
Net系统;
使用System.Net.Sockets;
使用System.IO;
名称空间chatter2
{
公共部分类Form1:Form
{
私人TCP客户;
公共图书馆;
公共图书馆;
公共字符串接收;
公共字符串TextToSend;
公共表格1()
{
初始化组件();
IPAddress[]localIP=Dns.GetHostAddresses(Dns.GetHostName());
foreach(本地IP中的IP地址)
{
if(address.AddressFamily==AddressFamily.InterNetwork)
{
ServerIPtextBox.Text=地址.ToString();
}
}
}
私有无效开始按钮单击(对象发送者,事件参数e)
{
TcpListener listener=newtcplistener(IPAddress.Any,int.Parse(ServerPorttextBox.Text));
listener.Start();
client=listener.AcceptTcpClient();
STR=newstreamreader(client.GetStream());
STW=新的StreamWriter(client.GetStream());
STW.AutoFlush=true;
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.WorkerSupportsScanCellation=true;
}
私有void ConnectButton\u单击(对象发送者,事件参数e)
{
client=新的TcpClient();
IPEndPoint IpEnd=新的IPEndPoint(IPAddress.Parse(clientptextbox.Text),int.Parse(ClientPorttextBox.Text));
尝试
{
client.Connect(IpEnd);
如果(客户端已连接)
{
ChatScreentextBox.AppendText(“已连接到服务器”+“\n”);
STW=新的StreamWriter(client.GetStream());
STR=newstreamreader(client.GetStream());
STW.AutoFlush=true;
backgroundWorker1.RunWorkerAsync();
backgroundWorker2.WorkerSupportsScanCellation=true;
}
}
捕获(例外情况除外)
{
Show(例如Message.ToString());
}
}
私有void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
while(client.Connected)
{
尝试
{
recieve=STR.ReadLine();
this.ChatScreentextBox.Invoke(新的MethodInvoker(委托)()
{
ChatScreentextBox.AppendText(“您:+recieve+”\n”);
}));
receive=“”;
}
捕获(例外情况除外)
{
Show(例如Message.ToString());
}
}
}
私有void backgroundWorker2_DoWork(对象发送方,DoWorkEventArgs e)
{
如果(客户端已连接)
{
STW.WriteLine(TextToSend);
this.ChatScreentextBox.Invoke(新的MethodInvoker(委托)()
{
ChatScreentextBox.AppendText(“我:+TextToSend+”\n”);
}));
}
其他的
{
MessageBox.Show(“发送失败”);
}
backgroundWorker2.CancelAsync();
}
私有无效发送按钮\单击(对象发送程序,事件参数e)
{
如果(MessagetextBox.Text!=“”)
{
TextToSend=MessagetextBox.Text;
backgroundWorker2.RunWorkerAsync();
}
MessagetextBox.Text=”“;
}
}
}
这一行正在阻塞您的UI线程

我会尝试使用async并等待

private async void StartButton_Click(object sender, EventArgs e)
    {
        TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(ServerPorttextBox.Text));
        listener.Start();
        client = await listener.AcceptTcpClientAsync();
        STR = new StreamReader(client.GetStream());
        STW = new StreamWriter(client.GetStream());
        STW.AutoFlush = true;

        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.WorkerSupportsCancellation = true;

    }

我希望它能帮助您。

您没有错误吗?不,它只是“滞后”没有什么是可以接受的,它只是一个崩溃,直到我停止程序。标记和崩溃不是一回事,您能定义滞后吗?GUI是否停止响应?另外,为什么在这两种方法中都运行backgroundWorker1,但却告诉backgroundWorker2支持取消?该按钮在UI线程上处理,并且
client=listener.AcceptTcpClient()正在阻塞。这意味着用户界面将被阻塞,直到连接被接受。@John,这是解决方案。它不是崩溃。实际上,它是正确的,它将等待连接完成。谢谢!现在我可以回到我自己的程序了。我想它是可以关闭的有命令吗#接近
private async void StartButton_Click(object sender, EventArgs e)
    {
        TcpListener listener = new TcpListener(IPAddress.Any, int.Parse(ServerPorttextBox.Text));
        listener.Start();
        client = await listener.AcceptTcpClientAsync();
        STR = new StreamReader(client.GetStream());
        STW = new StreamWriter(client.GetStream());
        STW.AutoFlush = true;

        backgroundWorker1.RunWorkerAsync();
        backgroundWorker2.WorkerSupportsCancellation = true;

    }