简体   繁体   English

STA中的Akka.net演员

[英]Akka.net actors in STA

I need to convert thousands of ms office documents in different formats into one common format. 我需要将成千上万种不同格式的Office文档转换为一种通用格式。 To get things faster i would parallelize it using akka.net for. 为了使事情更快,我将使用akka.net对其进行并行化。 The WordSaveAsActor should: WordSaveAsActor应该:

  • run in Single-Threaded Appartment 在单线程单元中运行
  • hold the Word Application instance 持有Word应用程序实例
  • make COM calls on this instance, like SaveAs(..) with paths from received messages from multiple parallel threads 在此实例上进行COM调用,例如SaveAs(..),具有来自多个并行线程的已接收消息的路径
  • restart itself by any crash 通过任何崩溃重新启动自身

Is it even possible to run akka.net actor in STA? 甚至可以在STA中运行akka.net actor吗? Is there any concerns if I use the akka.net this way? 如果我以这种方式使用akka.net,是否有任何担忧?

The short answer is "yes, you can do this with Akka.NET" 简短的答案是“是的,您可以使用Akka.NET做到这一点”

As Hans points out in his comment: 正如汉斯在评论中指出的那样:

Word doesn't care much about your library. Word不太在乎您的图书馆。 Its interop interface is single-threaded, any call you make from a worker thread is automatically marshaled to the thread that owns the Application object. 它的互操作接口是单线程的,您从工作线程进行的任何调用都会自动编组到拥有Application对象的线程中。

You can use the dispatchers in Akka.NET to exercise some control over your actors' concurrency needs - you can read about different types of Akka.NET dispatchers and how they work in our Akka.NET Bootcamp Lesson 2.1 . 您可以使用Akka.NET中的调度程序对参与者的并发需求进行一些控制-您可以在Akka.NET Bootcamp课程2.1中了解不同类型的Akka.NET调度程序以及它们的工作方式

In your case, here's what I'd recommend you do: 对于您而言,这是我建议您执行的操作:

  1. Create your WordSaveAsActor on the STA thread and configure it to use the CurrentSynchronizationContextDispatcher in Akka.NET, which you can do via configuration: 在STA线程上创建WordSaveAsActor并将其配置为使用Akka.NET中的CurrentSynchronizationContextDispatcher ,可以通过配置来完成:

var wordActor = MyActorSystem.ActorOf(Props.Create(() => new WordSaveAsActor(...)).WithDispatcher("akka.actor.synchronized-dispatcher"))

All messages this actor processes will be done automatically on the STA thread. 这个参与者处理的所有消息将在STA线程上自动完成。 We use this technique in other STA environments, such as Windows Forms UI, and it greatly simplifies everything. 我们在其他STA环境(例如Windows Forms UI)中使用了此技术,它大大简化了一切。

  1. Do all of the other work that doesn't require Word's COM component on other actors that use the default dispatcher (CLR threadpool.) This might include any file copy, open, move, etc.. When you need to send data to or receive data from word, you can have these actors pass messages to your WordSaveAsActor . 在使用默认调度程序(CLR线程池)的其他参与者上执行不需要Word的COM组件的所有其他工作。这可能包括任何文件的复制,打开,移动等。当您需要向或向其发送数据时来自单词的数据,您可以让这些参与者将消息传递到WordSaveAsActor

As Hans points out, unless you run multiple instances of Word in parallel you can't really do more than one operation at a time leveraging its COM API. 正如汉斯指出的那样,除非您并行运行Word的多个实例,否则利用它的COM API一次实际上不能做多个操作。 So in order to take advantage of Akka.NET's parallelism you'll need to find a way to use multiple actors to parallelize the parts of your operation that don't require Word. 因此,为了利用Akka.NET的并行性,您需要找到一种方法来使用多个参与者来并行化不需要Word的操作部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM