Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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#gui挂起,但仍然可以侦听和处理_C#_User Interface_Blocking - Fatal编程技术网

c#gui挂起,但仍然可以侦听和处理

c#gui挂起,但仍然可以侦听和处理,c#,user-interface,blocking,C#,User Interface,Blocking,为什么我的c#服务器gui挂起?知道我哪里出错了吗?多谢各位 就像我单击按钮1的那一刻,gui挂起,但它仍然可以处理请求并侦听和接受传入的客户端连接 public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } TcpListener listener = null; TcpCl

为什么我的c#服务器gui挂起?知道我哪里出错了吗?多谢各位

就像我单击按钮1的那一刻,gui挂起,但它仍然可以处理请求并侦听和接受传入的客户端连接

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    TcpListener listener = null;
    TcpClient client = null;
    NetworkStream stream = null;
    BinaryWriter writer = null;
    BinaryReader reader = null;
    string vouchercode;
    string username;
    string password;
    string reseller;
    string fresh;
    string result;


    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            listener = new TcpListener(new IPAddress(new byte[] {127,0,0,1}), 6666);
            listener.Start();
            while (true)
            {
                label1.Text = "waiting....";
                using (client = listener.AcceptTcpClient())
                {
                    label1.Text = "Connection request accepted!";
                    using (stream = client.GetStream())
                    {


                        //some codes here ..
                    }
                }
            }
        }


        catch (WebException ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if (listener != null) listener.Stop();
            if (writer != null) writer.Close();
            if (reader != null) reader.Close();
        }


    }



}

}

它挂起,因为它是一种阻塞方法。您可以查看并尝试合并,使其成为非阻塞的。msdn页面中有一个示例。

它挂起,因为它是一种阻塞方法。您可以查看并尝试合并,使其成为非阻塞的。msdn页面中有一个示例。

您也在输入一个
,而
循环中没有我可以看到的退出逻辑。所以你要被绞死。这也是一个很好的实践,让你的重物从你的活动中解脱出来,在这种情况下,进入一个不同的线程

您也正在输入一个
,而
循环中没有我可以看到的退出逻辑。所以你要被绞死。这也是一个很好的实践,让你的重物从你的活动中解脱出来,在这种情况下,进入一个不同的线程

当您在UI线程上进行处理时(就像您在按钮单击处理程序中一样),重要的是不要阻塞。正如Bala所指出的,您有一个阻塞调用,它位于一个(可能是无限的)循环中,这是一个问题,因为您永远不会返回函数,允许处理窗口消息(窗口消息做一些事情,比如重新绘制窗口,响应UI控件,比如按钮单击等等)

答案是使
按钮1\u单击
不阻塞,或者将套接字代码移动到其他线程

查看此SO线程:


当您在UI线程上进行处理时(就像您在按钮单击处理程序中一样),重要的是不要阻塞。正如Bala所指出的,您有一个阻塞调用,它位于一个(可能是无限的)循环中,这是一个问题,因为您永远不会返回函数,允许处理窗口消息(窗口消息做一些事情,比如重新绘制窗口,响应UI控件,比如按钮单击等等)

答案是使
按钮1\u单击
不阻塞,或者将套接字代码移动到其他线程

查看此SO线程:


您能解释一下如何使其不阻塞吗。上面的链接展示了如何使其无阻塞的示例。我推荐这种方法。你能解释一下如何使它不阻塞吗。上面的链接展示了如何使其无阻塞的示例。我建议采取这种做法。