当前位置:编程学习 > C#/ASP.NET >>

TCP Socket 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke

   /// <summary>
        /// 接收TCP协议连接
        /// </summary>
        private void AcceptConnection()
        {
            try
            {
                IPAddress[] iAddress = Dns.GetHostAddresses(Dns.GetHostName());
                IPAddress address = null;
                string ip = ReadAddressXML.GetReadIPAdress();
                string state = ReadAddressXML.sStatus;
                Int32 iPort = ReadAddressXML.sPort;
                if (state.Trim().Equals("close"))
                {
                    foreach (IPAddress item in iAddress)
                    {
                        if (item.IsIPv6LinkLocal == false && item.AddressFamily == AddressFamily.InterNetwork)
                        {
                            address = item;
                            break;
                        }
                    }
                }
                else
                    address = IPAddress.Parse(ip);


                if (address == null)
                    address = IPAddress.Parse(ip);


                tcpListener = new TcpListener(address, iPort); //该类在指定的本地 IP 地址和端口号上侦听是否有传入的连接尝试
                tcpListener.Start(); //开始侦听传入的连接请求
                txtStateInfo.Invoke(settextboxcallback, Dns.GetHostName() + "(" + address.ToString() + ")开始等待连接.......");
                tsNavigation.Invoke(settoolstripcallback, isExit);
                MResetEvent = new ManualResetEvent(false);
                while (isExit == false)
                {
                    try
                    {
                        MResetEvent.Reset(); //将事件状态设置为非终止状态,从而导致线程受阻
                        AsyncCallback callBack = new AsyncCallback(AcceptTcpClientCallBack); //委托


                        tcpListener.BeginAcceptTcpClient(callBack, tcpListener);
                        MResetEvent.WaitOne();
                    }
                    catch (Exception ex) { txtStateInfo.Invoke(settextboxcallback, ex.Message); break; }
                }
            }
            catch (Exception ex)
            {
               // if ((!this.IsDisposed) && (this != null) && (this.IsHandleCreated))
                    txtStateInfo.Invoke(settextboxcallback, ex.Message);
            }
        }


        /// <summary>
        /// 接收TCP客户端连接
        /// </summary>
        /// <param name="iar">异步操作状态</param>
        private void AcceptTcpClientCallBack(IAsyncResult iar)
        {
            try
            {
                if (isExit == false)
                {
                    MResetEvent.Set();
                    TcpListener myListen = iar.AsyncState as TcpListener;


                    TcpClient client = myListen.EndAcceptTcpClient(iar);
                    txtStateInfo.Invoke(settextboxcallback, "已接受客户连接:" + client.Client.RemoteEndPoint);
                    // dgvClient.Invoke(setlistboxcallback, client.Client.RemoteEndPoint.ToString());
                    TCPDataReadWrite dataReadWrite = new TCPDataReadWrite(client);
                    clientlist.Add(dataReadWrite.Client.Client.RemoteEndPoint.ToString(), dataReadWrite);
                    SendString(dataReadWrite, SendCommand.CONNECT_SUCCEE, "连接服务器成功!", "");


                    dataReadWrite.NWorkStream.BeginRead(dataReadWrite.ReadData, 0, dataReadWrite.ReadData.Length, ReadCallBack, dataReadWrite);
                }


            }
            catch (Exception ex) { txtStateInfo.Invoke(settextboxcallback, ex.Message);  }
        } --------------------编程问答-------------------- 这个什么问题呢? 怎么处理呢? 在关闭窗体时出现了 --------------------编程问答-------------------- 那么大的提示看不到
点详细信息
自己看堆栈
--------------------编程问答-------------------- --------------------编程问答-------------------- 问题:
未处理 InvalidOperaitonException
在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke

图片太了
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,