简体   繁体   English

这些事件回调会发生在什么线程上?

[英]What thread will these event callbacks occur on?

new System.Threading.Thread(() =>
    {
        var myObject = new CustomObject();
        myObject.SomeEvent += SomeMethod;
    }).Start();

Part 1: Assume I run the above code on my main UI thread in a winforms applications. 第1部分:假设我在winforms应用程序中的主UI线程上运行上面的代码。 Which Thread will calls to SomeMethod occur on? 哪个Thread会调用SomeMethod Will they occur on the same Thread that was created when I created the object? 它们是否会出现在我创建对象时创建的同一个Thread上?

Part 2: Using Visual Studio 2010, how can I figure this out on my own? 第2部分:使用Visual Studio 2010,我如何自己解决这个问题? I don't know where you find out what Thread something is running on. 我不知道你在哪里找到Thread运行的东西。

The event handler SomeMethod will run on whatever thread raised the event. 事件处理程序SomeMethod将在引发事件的任何线程上运行。

You can set a breakpoint in the event handler SomeMethod and look at the Threads window (you will probably want to give your threads meaningful names to find them). 您可以在事件处理程序SomeMethod设置断点并查看“ 线程”窗口 (您可能希望为线程提供有意义的名称以查找它们)。

To find out which thread you are currently executing in, you can use Thread.CurrentThread.ManagedThreadId . 要找出当前正在执行的线程,可以使用Thread.CurrentThread.ManagedThreadId

To answer your question: SomeMethod will be executed in the thread that raises the event. 回答你的问题:SomeMethod将在引发事件的线程中执行。

In your case, it actually will never be executed as you assign your method to the event but never actually raise the event. 在您的情况下,它实际上永远不会执行,因为您将方法分配给事件但从未实际引发事件。

you may check the ManagedThreadId Property http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx 您可以查看ManagedThreadId属性http://msdn.microsoft.com/en-us/library/system.threading.thread.managedthreadid.aspx

@Mike Here is the sample @Mike这是样本

        Console.WriteLine("Main New Thread : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);

        new System.Threading.Thread(() =>
        {
            Console.WriteLine("Inside New Thread : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
        }).Start();

        Console.WriteLine("Main New Thread : {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);

Just wanted to add a simple test that will allow you to check the thread on which the handler executes: 只是想添加一个简单的测试,它将允许您检查处理程序执行的线程:

class Test
{
    delegate void update();
    static event update updateEvent;

    static void Main(string[] args)
    {
        Console.WriteLine("Parent thread: " + Thread.CurrentThread.ManagedThreadId);
        updateEvent += new update(Test_updateEvent);
        var t = new Thread(
            () =>
            {
                Console.WriteLine("Child thread: " + Thread.CurrentThread.ManagedThreadId);
                updateEvent();
            });
        t.Start();
        t.Join();
    }

    static void Test_updateEvent()
    {
        Console.WriteLine("Event thread: " + Thread.CurrentThread.ManagedThreadId);
    }
}

which prints: 打印:

Parent thread: 1
Child thread: 3
Event thread: 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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