在主程序运行时,若进入死循环,或者忙着执行某一程序,导致后面的代码没办法执行 可以用多线程解决
1.代码
Thread th1 = new Thread(Resive); th1.Start(s); //start只是告诉cpu可以执行了 具体执行时间得看cpu th1.IsBackground = true; //变为后台线程
2.注意
前台线程:只有所有的前台线程全部结束程序才会结束
后台线程: 前台线程一结束,后台跟着结束
主线程与 自己写的多线程一般默认为前台线程 可通过
th1.IsBackground = true; 变为后台线程
3.带有参数的方法
Thread th = new Thread(Listen); th.IsBackground = true;// th.Start(so);// void Listen(object o) //引用类型 { Socket so = o as Socket;
该方法的参数必须时object型,根据里氏转换 可以 强转为子类
形参必须通过start传
原文:https://www.cnblogs.com/pursuit-purity/p/14633833.html