【发布时间】:2018-07-11 18:57:12
【问题描述】:
我有一个关于线程对象的问题。 假设我在执行 'NewThread()' 方法的 'pendingThread' 上创建了一个新的 Thread 对象并启动它。 NewThread() 方法需要相当长的时间才能返回。如果在先前启动的线程返回之前重新初始化“pendingThread”会发生什么? 它会中止还是暂停?
很高兴看到你的回答
public void Threaded_accept()//this function accepts client. It's executed on the new thread
{
bool pending = this.listen_socket.AcceptAsync(this.accept_args);// If completed Asynchronously
//On_Accept_Completed is called Automatically
if (pending == false)// If AcceptAsync was completed synchronously
{
this.pendingThread = new Thread(StartNewThread);
pendingThread.Start();//This is for keep receiving requests while Thread is working
//TODO What happens when pendingThread is reinitialized while pending Thread was running?
}
flow_control_event.WaitOne();//wait until scoket is accepted
}
【问题讨论】:
-
线程对象在执行代码时不能消失。与普通的 .NET 对象不同,不需要对象引用,因为 CLR 本身知道它。它知道线程何时启动和停止执行代码。同样值得注意的是 Thread 没有 Dispose() 方法,即使它使用 5 个非托管 OS 对象。与 Task 对象不同的是,它们也不能消失,而是通过实际的对象引用完成。存储在堆栈中,GC 总能找到它。
标签: c# .net multithreading