<tfoot id='bJl2Z'></tfoot>

    <i id='bJl2Z'><tr id='bJl2Z'><dt id='bJl2Z'><q id='bJl2Z'><span id='bJl2Z'><b id='bJl2Z'><form id='bJl2Z'><ins id='bJl2Z'></ins><ul id='bJl2Z'></ul><sub id='bJl2Z'></sub></form><legend id='bJl2Z'></legend><bdo id='bJl2Z'><pre id='bJl2Z'><center id='bJl2Z'></center></pre></bdo></b><th id='bJl2Z'></th></span></q></dt></tr></i><div id='bJl2Z'><tfoot id='bJl2Z'></tfoot><dl id='bJl2Z'><fieldset id='bJl2Z'></fieldset></dl></div>

      <bdo id='bJl2Z'></bdo><ul id='bJl2Z'></ul>

    1. <legend id='bJl2Z'><style id='bJl2Z'><dir id='bJl2Z'><q id='bJl2Z'></q></dir></style></legend>
    2. <small id='bJl2Z'></small><noframes id='bJl2Z'>

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

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

        1. <i id='3wzSp'><tr id='3wzSp'><dt id='3wzSp'><q id='3wzSp'><span id='3wzSp'><b id='3wzSp'><form id='3wzSp'><ins id='3wzSp'></ins><ul id='3wzSp'></ul><sub id='3wzSp'></sub></form><legend id='3wzSp'></legend><bdo id='3wzSp'><pre id='3wzSp'><center id='3wzSp'></center></pre></bdo></b><th id='3wzSp'></th></span></q></dt></tr></i><div id='3wzSp'><tfoot id='3wzSp'></tfoot><dl id='3wzSp'><fieldset id='3wzSp'></fieldset></dl></div>
          <tfoot id='3wzSp'></tfoot>
          <legend id='3wzSp'><style id='3wzSp'><dir id='3wzSp'><q id='3wzSp'></q></dir></style></legend>

          1. <small id='3wzSp'></small><noframes id='3wzSp'>

            • <bdo id='3wzSp'></bdo><ul id='3wzSp'></ul>

                <tbody id='3wzSp'></tbody>
                本文介绍了.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 对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                Accessing attribute info from DTE(从 DTE 访问属性信息)
                c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                C# Attributes and their uses(C# 属性及其用途)
                C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                • <bdo id='QVvQn'></bdo><ul id='QVvQn'></ul>
                  <i id='QVvQn'><tr id='QVvQn'><dt id='QVvQn'><q id='QVvQn'><span id='QVvQn'><b id='QVvQn'><form id='QVvQn'><ins id='QVvQn'></ins><ul id='QVvQn'></ul><sub id='QVvQn'></sub></form><legend id='QVvQn'></legend><bdo id='QVvQn'><pre id='QVvQn'><center id='QVvQn'></center></pre></bdo></b><th id='QVvQn'></th></span></q></dt></tr></i><div id='QVvQn'><tfoot id='QVvQn'></tfoot><dl id='QVvQn'><fieldset id='QVvQn'></fieldset></dl></div>

                      <tbody id='QVvQn'></tbody>

                      1. <legend id='QVvQn'><style id='QVvQn'><dir id='QVvQn'><q id='QVvQn'></q></dir></style></legend>

                        <small id='QVvQn'></small><noframes id='QVvQn'>

                          <tfoot id='QVvQn'></tfoot>