.NET 4 任务并行库可以使用 COM 对象吗? [英] Can the .NET 4 Task Parallel Library use COM objects?

查看:16
本文介绍了.NET 4 任务并行库可以使用 COM 对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个这可能吗?如果可以,你能给我一个简单的例子吗,因为我在网上找不到一个?"类似的问题.

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 中的 Task Parallel 库使用 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).

这可能吗?谢谢.

推荐答案

在 TPL 中使用 COM 对象是 100% 可能的.虽然默认情况下,TPL 将使用标准的 .NET 线程池,但 TPL 通过 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 线程都是 初始化为 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.

虽然 TaskScheduler 编写起来并不简单,但如果您对线程有基本的了解,编写它们也不是那么难.幸运的是 ParallelExtensions Extras 库 已经提供了一个 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 初始化为静态,然后只需启动您的 Tasks 并指定它们是由那个例子.看起来像这样:

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);

这篇关于.NET 4 任务并行库可以使用 COM 对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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