none
跨线程访问或者终止socket.accept()的问题 RRS feed

  • 问题

  • 我现在在一个后台任务backgroudworker中开了一个while()循环,循环中有一个服务端socket在侦听socket连接并Accept,然后希望能够通过一个按钮来控制侦听任务的开启和关闭,但是现在的问题是接受到一个socket后完成一次循环然后一直停在Accept处,也点击也无法停止backgroudworker,如果能在按钮点击事件中关闭服务端socket或者终止socket.accept(),但不知怎么实现,或者是否有其他方法来达到目标。
            private void buttonServerSet_Click(object sender, EventArgs e)
            {
                
                if (buttonServerSet.Text == "建立服务端")
                {
                    buttonServerSet.Text = "停止服务端";
                    LocalseverPort.Enabled = false;
                    string Port = LocalseverPort.Text;
                    backgroundWorker1.RunWorkerAsync(Port);
                }
                else
                {
                    buttonServerSet.Text = "建立服务端";
                    LocalseverPort.Enabled = true;
                    ClientList.Items.Clear();
                    backgroundWorker1.CancelAsync();
                    backgroundWorker1.Dispose();
                }
     private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                server.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(e.Argument)));
                int p = 0;
                Socket client = null;
                            server.Listen(10);
                            while (true)
                            {
                                if (backgroundWorker1.CancellationPending)
                                    break;
                                try
                                {
                                    client = server.Accept();
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Listen error :" + ex.Message);
                                }
                                backgroundWorker1.ReportProgress(p,client);
                            }
            }


    • 已编辑 hpirate 2015年7月31日 8:02
    • 已移动 Youjun Tang 2015年8月3日 5:42 more appropriate
    2015年7月31日 7:56

答案

  • 你好,

    试试用e.cancel=true

      if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月4日 1:44

全部回复

  • 你好,

    这是VB.NET论坛,你的问题更多的是关于C# winform的内容,为了帮你获得更好的支持,我会把你的帖子移到C#论坛


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月3日 5:39
  • 你好,

    试试用e.cancel=true

      if (worker.CancellationPending == true)
                    {
                        e.Cancel = true;
                        break;
                    }
    


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年8月4日 1:44