简体   繁体   English

使用BeginInvoke时,是否可以确定执行处理程序的线程?

[英]Is there a way to determine the thread in which the handler is executed when using BeginInvoke?

I want to determine the thread in which the handler is executed when using BeginInvoke. 我想确定使用BeginInvoke时在其中执行处理程序的线程。 Right now each time I invoke the method the handler gets executed by a different thread. 现在,每次我调用该方法时,处理程序都会由不同的线程执行。 Is there a way to determine the thread? 有没有确定线程的方法?

using System;
using System.Threading;
using System.Runtime.Remoting.Messaging;

namespace Delegate
{
  public delegate void BarDelegate(string argument);

  public class Foo
  {
    private void handleBar(string argument)
    {
      int threadId = AppDomain.GetCurrentThreadId();
      Console.WriteLine("Argument is " + argument + " " + threadId.ToString());
    }

    public void bar(string argument)
    {
      BarDelegate smd = new BarDelegate(this.handleBar);
      smd.BeginInvoke(argument, null, null);
    }

    public static void Main()
    {
      Console.WriteLine(AppDomain.GetCurrentThreadId().ToString());
      Foo ds = new Foo();
      ds.bar("Hello, world!");
      Console.ReadLine();
      ds.bar("Hello, world!");
      Console.ReadLine();
    }
  }
}

Getting asynchronous code to execute on a specific thread requires a synchronization provider. 使异步代码在特定线程上执行需要同步提供程序。 There are two main ones in the .NET framework, WindowsFormsSynchronizationContext which helps Control.BeginInvoke() to run code on the UI thread. .NET框架中有两个主要的控件:WindowsFormsSynchronizationContext,它可以帮助Control.BeginInvoke()在UI线程上运行代码。 And DispatcherSynchronizationContext, supporting Dispatcher.BeginInvoke() for WPF. 和DispatcherSynchronizationContext,为WPF支持Dispatcher.BeginInvoke()。 These providers are automatically installed when you call Application.Run() for these class libraries. 当您为这些类库调用Application.Run()时,将自动安装这些提供程序。 They can only invoke to the UI thread. 他们只能调用UI线程。

There is no sync provider for a console mode program. 控制台模式程序没有同步提供程序。 And a delegate's BeginInvoke() method always executes on a threadpool thread. 委托的BeginInvoke()方法始终在线程池线程上执行。 If you cannot use these class libraries and the component you use has no support for multi-threading (very few do, unless it is a COM server) then using threading is not an option available to you. 如果您不能使用这些类库,并且所使用的组件不支持多线程(很少支持,除非它是COM服务器),那么就不可以使用线程。

This is because BeginInvoke uses the famous AppDomain's ThreadPool . 这是因为BeginInvoke使用著名的AppDomain的ThreadPool

If you need to know which thread executes it - and you really really need to - then perhaps you need to create you own threads. 如果您确实需要执行该线程的线程- 确实需要-也许您需要创建自己的线程。 ThreadPool (and for that matter BeginInvoke ) is when we need something to be done and not caring how and when. ThreadPool (就此而言, BeginInvoke就是在什么时候)我们需要做一些事情,而不在乎如何和何时进行。

I think what you really want is having control of in which thread the async task is running. 我认为您真正想要的是控制异步任务在哪个线程中运行。 You can't use BeginInvoke, because it makes the task run in a random thread from the .NET ThreadPool. 您不能使用BeginInvoke,因为它使任务在.NET ThreadPool的随机线程中运行。 So, you need to create a thread first, then post task to it, in this way you have control. 因此,您需要先创建一个线程,然后将任务发布到该线程,这样您就可以控制。 If you don't want to write your own, I recommend you use the SmartThreadPool, just google it. 如果您不想自己编写,我建议您使用SmartThreadPool,只需在Google上进行搜索即可。

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

相关问题 在线程中使用Dispatcher.BeginInvoke时出现奇怪的线程异常 - Weird Thread exception when using Dispatcher.BeginInvoke in a thread 使用BeginInvoke在后台线程上执行 - Executing on a background thread using BeginInvoke 如何确定使用TPL时方法将执行哪个线程? - How to determine which thread a method will executes on when using TPL? 哪个线程是一个BeginInvoke的异步委托的回调? - which thread a BeginInvoke's callback of a asynchronous delegate is in? 是否可以使用ISynchronizeInvoke.BeginInvoke()重载线程? - Is it possible to overload a thread using ISynchronizeInvoke.BeginInvoke()? 等待Dispatcher.BeginInvoke…(使用WPF和WCF)时,我的UI线程被阻止了 - My UI thread is blocked when await Dispatcher.BeginInvoke… (using WPF and WCF) 在mvvm wpf中使用Application.Current.Dispatcher.BeginInvoke启动新线程时,弹出窗口无法打开 - Pop up not getting opened when a new thread is started using Application.Current.Dispatcher.BeginInvoke in mvvm wpf 可以指定使用delegate.BeginInvoke时AsyncCallback运行的线程吗? - Can one specify the thread that the AsyncCallback runs on when using delegate.BeginInvoke? 为什么在使用Dispatcher.BeginInvoke()时会出现“无效的跨线程访问”? - Why do I get “Invalid cross-thread access” when using Dispatcher.BeginInvoke()? 没有执行BeginInvoke - BeginInvoke not being executed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM