Can the .NET 4 Task Parallel Library use COM objects?(.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 对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET 4 任务并行库可以使用 COM 对象吗?
基础教程推荐
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01