线程

  • 线程

前台线程和后台线程:应用程序必须运行完所有的前台线程才可以退出;而对于后台线程,应用程序则可以不考虑其是否已经运行完毕而直接退出,所有的后台线程在应用程序退出时都会自动结束。

public void Send()
        {
            int i = 7;
            while (i>0)
            {
                txt.Text += "hello\n";
                Thread.Sleep(2000);
                i--;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread p = new Thread(Send);
            p.IsBackground = false;
            p.Start();
        }

 根据前台线程的特性,只有等所有的前台线程完成应用程序才会退出,所以运行该程序,并立马关闭窗口,立即打开任务管理器,观察到应用程序进程还存在,不过等到我们设置的前台线程运行完,大概14秒,就会发现应用程序进程突然不见了

等待之后

这就是前台线程和后台线程最直观的区别。

 用在网络编程中,需要保证服务端和客户端的稳定性,至少拥有友好的界面提示,不能直接死机和崩溃

 一套比较友好的服务器和客户端源码:https://files.cnblogs.com/HelloMyWorld/socket%E5%92%8C%E7%BA%BF%E7%A8%8B%E7%BC%96%E7%A8%8B%E8%AF%A6%E7%BB%86.rar

服务端:

class Program
    {
        static private void Rec(object myclient)
        {
            Socket client = (Socket)myclient;
            byte[] buffer = new byte[1024];
            string Data = "";
            while (true)
            {
                try
                {
                    int number=client.Receive(buffer);
                    Console.WriteLine("Rec OK....");
                    Data = Encoding.UTF8.GetString(buffer,0,number);
                    Console.WriteLine(Data);
                }
                catch (SocketException sx)
                {
                    Console.WriteLine(sx.SocketErrorCode);
                    if (sx.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        break;
                    }
                }
            }
            Console.WriteLine("server client close");
            client.Close();
            //Environment.Exit(0);
        }

        static void Main(string[] args)
        {

            try
            {
                Thread.CurrentThread.IsBackground = true;
                Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint serverip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
                server.Bind(serverip);
                server.Listen(3);
                Console.WriteLine("Listen....");
                while (true)
                {
                    Socket client = server.Accept();
                    Thread tRec = new Thread(Rec);
                    tRec.Start((object)client);
                    Console.WriteLine("tRec Start....");
                }
            }
            catch (SocketException sx)
            {
                Console.WriteLine(sx.ErrorCode);
                
            }
        }
    }

客户端:

class Program
    {
        static void Snd(object myclient)
        {
            Socket client = (Socket)myclient;
            int i = 0;
            while (true)
            {
                try
                {
                    string Data = "Hello,I'am the "+i.ToString();
                    i++;
                    int buffernumber = Encoding.UTF8.GetByteCount(Data);
                    byte[] buffer = new byte[buffernumber];
                    buffer = Encoding.UTF8.GetBytes(Data);
                    client.Send(buffer);
                    Thread.Sleep(1000);
                }
                catch (SocketException sx)
                {
                    Console.WriteLine(sx.ErrorCode);
                    if (sx.SocketErrorCode == SocketError.ConnectionReset)
                    {
                        break;
                    }
                }
            }
            Console.WriteLine("client Close");
            client.Close();
        }

        static void Main(string[] args)
        {
            Socket client = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            try
            {
                client.Connect(IPAddress.Parse("127.0.0.1"), 1234);
                Thread tSnd = new Thread(Snd);
                tSnd.Start((object)client);
            }
            catch(SocketException sx)
            {
                Console.WriteLine(sx.ErrorCode);
                if (sx.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    Console.WriteLine("服务器未开....");
                    client.Close();
                }
                
            }
            
        }
    }
原文地址:https://www.cnblogs.com/HelloMyWorld/p/2994564.html