Beispiel #1
0
        void RunOnAnotherThread2()
        {
            try
            {
                udpServer = new UdpClient(serverPort);

                while (true)
                {
                    var    remoteEp = new IPEndPoint(IPAddress.Any, serverPort);
                    Byte[] data     = udpServer.Receive(ref remoteEp);
                    string rcvdData = Encoding.ASCII.GetString(data);
                    if (rcvdData == "ConnectionRequest")
                    {
                        //txtRcvMsg = "receive data from " + remoteEp.ToString();
                        //Application.Run(new FormChat(remoteEp.ToString() , "UserReceiver"));
                        // new Thread(() => new FormChat(remoteEp.ToString(), "UserReceiver").ShowDialog()).Start();
                        Thread t = new Thread(new ThreadStart(() =>
                        {
                            FormChat chtFrm = new FormChat(remoteEp.ToString(), "UserReceiver");
                            chtFrm.ShowDialog();
                        }
                                                              ));
                        t.SetApartmentState(ApartmentState.STA);
                        t.IsBackground = true;
                        t.Start();
                    }
                    //FormChat chtFrm = new FormChat(remoteEp.ToString());
                    //chtFrm.Show();
                }

                myEvent.Set();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }