[英]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.