STA中的Akka.net演员 [英] Akka.net actors in STA

查看:80
本文介绍了STA中的Akka.net演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将成千上万种不同格式的Office文档转换为一种通用格式.为了使事情更快,我将使用akka.net对其进行并行化. WordSaveAsActor应该:

I need to convert thousands of ms office documents in different formats into one common format. To get things faster i would parallelize it using akka.net for. The WordSaveAsActor should:

  • 在单线程单元中运行
  • 保留Word应用程序实例
  • 在此实例上进行COM调用,例如SaveAs(..),具有来自多个并行线程的已接收消息的路径
  • 在发生任何崩溃时自动重启

是否甚至可以在STA中运行akka.net actor?如果我以这种方式使用akka.net,是否有任何疑问?

Is it even possible to run akka.net actor in STA? 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"

就像汉斯在评论中指出的那样:

As Hans points out in his comment:

Word不太在乎您的图书馆.它的互操作接口是单线程的,您从辅助线程进行的任何调用都会自动编组到拥有Application对象的线程中.

Word doesn't care much about your library. 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.

您可以使用Akka.NET中的调度程序对演员的并发需求进行一些控制-您可以

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.

根据您的情况,这是我建议您执行的操作:

In your case, here's what I'd recommend you do:

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

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

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

此参与者处理的所有消息将在STA线程上自动完成.我们在其他STA环境(例如Windows Forms UI)中使用了此技术,它大大简化了一切.

All messages this actor processes will be done automatically on the STA thread. We use this technique in other STA environments, such as Windows Forms UI, and it greatly simplifies everything.

  1. 在使用默认调度程序(CLR线程池)的其他参与者上执行不需要Word的COM组件的所有其他工作.这可能包括任何文件的复制,打开,移动等.当您需要发送数据时要从文字中接收数据或从文字中接收数据,您可以让这些参与者将消息传递到您的WordSaveAsActor.

正如汉斯指出的那样,除非您并行运行Word的多个实例,否则利用它的COM API一次不能真正执行多个操作.因此,为了利用Akka.NET的并行性,您需要找到一种使用多个参与者来并行化不需要Word的操作部分的方法.

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. 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.

这篇关于STA中的Akka.net演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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