可以在.NET 4任务并行库使用COM对象?对象、任务、NET、COM

2023-09-07 11:33:54 作者:风透绣罗衣

这是一个这是可能的,如果是的话你能不能给我一个简单的例子,因为我无法找到一个上网?样的问题。

This is an "is this possible, and if so can you give me a quick example because I can't find one online?" kind of question.

我有一个数字,我想用任务并行库在.NET Framework 4中使用C#并行运行完全独立的(即易并行)过程。一些过程需要使用的软件,可以通过COM / OLE自动进行访问。

I have a number of completely separate (i.e. "embarrassingly parallel") processes that I want to run in parallel using the Task Parallel library in .NET Framework 4 using C#. Some of these processes require the use of software that can be accessed via COM/OLE automation.

具体地,有一个Parallel.Foreach()循环划分向上的任务从项目的​​列表,基本上调用Parallel.Foreach内不同的功能来处理的处理(因此这些功能的一些采用COM库起作用)。

Specifically, there is a Parallel.Foreach() loop that divides up the tasks from a list of items, basically calling a different function inside the Parallel.Foreach to handle the processing (so some of these functions employ COM libraries to work).

这可能吗?谢谢你。

推荐答案

这是100%可以使用COM对象与第三方物流。虽然这是真的,默认情况下,第三方物流将使用标准的.NET线程池,第三方物流必须通过的的的TaskScheduler 类它使您能够提供自己的调度器可以调度工作,你所创建的线程。

It's 100% possible to use COM objects with the TPL. While it's true that, by default, the TPL will use the standard .NET ThreadPool, the TPL has an extension point via the TaskScheduler class which enables you to provide your own scheduler which can dispatch work to threads which you've created.

在使用的COM对象,您首先需要知道,如果COM类需要STA线程和MTA线程的情况下。如果MTA线程,那么有没有什么特别的需要进行,因为COM类已经可以从任何随机线程中使用。不幸的是最经典的COM对象往往依靠STA线程,这时候你需要使用一个自定义的的TaskScheduler 所以,无论.NET线程您使用的是他们已经initialized作为一个STA兼容线程。

In the case of of using COM objects you first need to know if the COM class requires STA threading or MTA threading. If MTA threading, then there's nothing special that needs to be done because the COM class can already be used from any random thread. Unfortunately most classic COM objects tend to rely on STA threading and that's when you'd need to employ a custom TaskScheduler so that whatever .NET thread you're using them from has been initialized as an STA compatible thread.

虽然TaskSchedulers并不完全微不足道的写,他们不是真的那么难写或者如果你有螺纹的一个基本的了解。幸运的是的ParallelExtensions附加库已经提供了一个 StaTaskScheduler 类所以你甚至不需要自己写任何东西。有一个伟大的博客文章由PFX团队讨论了实施和一些用例的 StaTaskScheduler 类。

While TaskSchedulers are not exactly trivial to write, they're not really that hard to write either if you've got a basic understanding of threading. Luckily the ParallelExtensions Extras library already provides an StaTaskScheduler class so you don't even need to write anything yourself. There's a great blog post here by the PFX team that discusses the implementation of and some use cases for the the StaTaskScheduler class.

不过基本上,你要初始化一个新的 StaTaskScheduler 作为您的类的一个地方一个静态的,然后就开始你的任务规定,他们计划通过该实例。这将是这个样子:

Basically though, you'll want to initialize a new StaTaskScheduler as a static somewhere on one of your classes and then just start your Tasks specifying that they are scheduled by that instance. That would look something like this:

// Create a static instance of the scheduler specifying some max number of threads
private static readonly StaTaskScheduler MyStaTaskScheduler = new StaTaskScheduler(4);

....

// Then specify the scheduler when starting tasks that need STA threading
Task.TaskFactory.StartNew(
() =>
{
    MyComObject myComObject = new MyComObject();

    myComObject.DoSomething();

    // ... etc ...
},
CancellationToken.None,
TaskCreationOptions.None,
MyStaTaskScheduler);