简体   繁体   中英

Reference to main thread in C#

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.

您可以在创建另一个线程时存储对Thread.CurrentThread (这将是主线程)的引用。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM