引用C#中的主线程 [英] Reference to main thread in C#

查看:398
本文介绍了引用C#中的主线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从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 ?

我想到了这种方式:在Visual Studio 2010中,您可以在调试时检查线程信息.线程窗口中的列之一是类别,它指定线程是工作线程还是主线程.是否可以获取此信息?

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 ?

谢谢.

推荐答案

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

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

这篇关于引用C#中的主线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆