奇怪的问题 thread里面使用了await,然后abort..
奇怪的问题 thread里面使用了await,然后abort..
卜霞森 发表于3年前
奇怪的问题 thread里面使用了await,然后abort..
  • 发表于 3年前
  • 阅读 20
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

摘要: thread abort await ObjectDisposedException

如题,自己做小工具,面向单个socket的tcp连接,使用thread处理TcpListener以及后续的数据接收,中间用到了await 和AcceptSocketAsync,然后我想在正在等待的时候取消listener状态。

目前使用的方式为直接thread.abort

但是在捕获到System.ObjectDisposedException

大致是说System.Net.Socket对象在企图对ta执行Dispose的时候已经被释放..



    class TcpServer_view_module : INotifyPropertyChanged
    {
...
        private TcpListener tcp_listener = null;
        private Socket tcp_socket = null;
...
        private async void thread_process_tcp_listener()
        {
            try
            {
                while (true)
                {
                    state = state_tcp_serve.waiting_client;
                    tcp_listener.Start(1);
                    tcp_socket = await tcp_listener.AcceptSocketAsync();
                    tcp_listener.Stop();
                    state = state_tcp_serve.connected;

                    if (tcp_socket != null)
                    {
                        while (true)
                        {
                            byte[] buffer = new byte[2048];
                            int len = tcp_socket.Receive(buffer);
                            if (len == 0)
                            {   //socket disconnect
                                break;
                            }
                            else
                            {
                                DataReceived(buffer, len);
                            }
                            tcp_socket.Dispose();
                        }
                    }
                }
            }
            catch (System.ObjectDisposedException dispose_e)
            {
...
            }
            catch (SystemException se)
            {
...
                state = state_tcp_serve.not_started;
            }
        }
...
        private void on_click()
        {
                switch (state)
                {
                    case state_tcp_serve.not_started:
...
                        thread_listener = new Thread(new ThreadStart(thread_process_tcp_listener));
                        thread_listener.Start();
                        break;

                    case state_tcp_serve.waiting_client:
                        tcp_listener.Stop();
                        if (thread_listener != null)
                        {
                            thread_listener.Abort();
                        }
                        if (tcp_socket != null)
                        {
                            tcp_socket.Dispose();
                            tcp_socket = null;
                        }
                        state = state_tcp_serve.not_started;
                        break;

                    case state_tcp_serve.connected:
                        state = state_tcp_serve.not_started;
                        if (thread_listener.IsAlive)
                        {
                            thread_listener.Abort();
                        }
                        if (tcp_socket != null)
                        {
                            tcp_socket.Dispose();
                            tcp_socket = null;
                        }
                        break;
                }            
        }


很奇怪的问题,大概有几个怀疑的点:

1、await虽然是语法糖,但是实际上还是拆解成异步来处理的,所以会导致异步操作未完成

2、

共有 人打赏支持
粉丝 1
博文 47
码字总数 32877
×
卜霞森
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: