c# 委托初窥

1.委托可以把方法当作参数在另一个方法中传递和调用 ,委托是方法的快捷方式。

2.委托是一个类。

private void BeginSocketThread()
        {
            try
            {
                IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7060);
                socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                byte[] byteMessage = new byte[1024];
                socketServer.Bind(iep);

                // do 
                while (true)
                {
                    socketServer.Listen(5);

                    Socket newSocket = socketServer.Accept();

                    newSocket.Receive(byteMessage);
                    if (DBConnection.mode_ip == DBConnection.MODE_LOCAL)
                    {
                        MessageBox.Show(LocRM_controlForm.GetString("请选择数据中心工作模式"));
                        continue;
                    }

                    string socketSeriesID = Encoding.Default.GetString(byteMessage).Split('E')[0];

                    FormImageList formImageListA = new FormImageList();
                    formImageListA.SeriesIndex = 0;

                    formImageListA.Owner = this;

                    //   formImageListA.OpenSeries(socketSeriesID);
                    aaaaaaaaa(socketSeriesID);
                    //if (InvokeRequired)
                    //{
                    //    SetTextCallback md = new SetTextCallback(formImageListA.OpenSeries);
                    //    //this.Invoke(md, new object[] { socketSeriesID });
                    //    //SetTextCallback md = new SetTextCallback(formImageListA.OpenSeries);
                    //    //this.Invoke(md, new object[] { socketSeriesID });
                    //}
                    //else
                    //{
                    //    formImageListA.OpenSeries(socketSeriesID);
                    //}

                    formImageListA.Dispose();
                }
            }

            catch (Exception ex)
            {
                MessageBox.Show("normal exp :"+ex.Message + "\r\n" +ex.StackTrace);
            }
        }

        private void aaaaaaaaa(string recvStr)
        {
            if (InvokeRequired)
            {
                SetTextCallback set = new SetTextCallback(aaaaaaaaa);
                Invoke(set, recvStr); 
            }
            else
            {
                FormImageList formImageListA = new FormImageList();
                formImageListA.SeriesIndex = 0;

                formImageListA.Owner = this;

                formImageListA.OpenSeries(recvStr);
                formImageListA.Dispose();
            }
        }

 

posted @ 2018-07-17 19:44  风影我爱罗  阅读(136)  评论(0编辑  收藏  举报