preface:
线程 被定义为程序的执行路径,每个线程执行特定的工作。当C#程序开始时,主线程自动创建。
-
线程生命周期:
- 未启动状态
- 就绪状态
- 不可运行状态
- 死亡状态
-
无参线程创建示例代码:
main(){
Thread threadAccept = new Thread(new ThreadStart(acceptThread));
threadAccept.start();
}
void acceptThread(){
//TODO
}
- 有参线程创建示例代码:
main(){
TcpClient tcpClient = new TcpClient();
tcpClient.Connect("192.168.0.1", 12138);
Thread threadAccept = new Thread(new ParameterizedThreadStart(reciveThread));
threadAccept.start(tcpClient);
}
void reciveThread(object tcpClient){
TcpClient client = parm as TcpClient;
//TODO
}