Reference to main thread in C#

情到浓时终转凉″ 提交于 2019-12-10 17:47:41

问题


How do I get a reference to the main thread from another thread in C# ?

Why this is needed ? Take for example an assembly loaded to a console application. This assembly raises another thread that wants to wait (join) on the main thread of the console application. How will it do that ?

I thought of this way: In visual studio 2010 you can inspect threads information while debugging. One of the columns in the threads window is the category that specifies if the thread is a worker thread or main thread. Is it possible to get this information ?

Or this way: What about getting the name of the function which is the entry point (main thread ?) and than iterate all threads in current process. How can I obtain this information ?

Thanks.


回答1:


You can store a reference to Thread.CurrentThread (which would be the main thread) in creation of another thread.




回答2:


No thread is actually main. After you spawn more threads it stops being important which one is main. The only way to find out which thread was created first is to save reference to it in some global static field.



来源:https://stackoverflow.com/questions/4845372/reference-to-main-thread-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!