VSTO与Windows窗体和工作者线程 [英] VSTO with Windows Form and Worker Threads

查看:576
本文介绍了VSTO与Windows窗体和工作者线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Office插件(如果Outlook有差别)与VSTO创建。该插件在启动时创建一个Windows窗体。我的形式创建多个线程。我在寻找任何具体的指导或人民什么安全适用于以下几种情况下的经验:



1)由表单创建一个线程需要访问Office对象模型(Globals.ThisAddIn.Application)



2)的形式创建的线程并不需要访问Office对象模型,但需要更新控制Windows窗体上(该表格是由插件或创建UI线程,因为我有时会听到它简称)



有关1)上面我已经采取了以下 https://msdn.microsoft.com/ EN-US /库/ 8sesy69e%28V = vs.120%29.aspx 意味着它是安全的,只要你线程的单元状态设置为STA和你处理异常。但的http://博客。 asp.net/whaggard/all-outlook-object-model-calls-run-on-the-main-thread 似乎暗示,在.NET VSTO,调用对象模型从任何后台线程是安全的它们会自动编组到主线程你,使背景STA线程仅仅是出于性能的考虑。是所有有给它?



有关2)是否与让线是一个任务或的IsBackground螺纹的任何问题,只要它使用控制的InvokeRequired /调用模式呢?或者它需要一个STA线程做调用?



更新
我见过几个VSTO专家提不触摸除主线程以外的任何其他Outlook对象模型和Outlook 2013中,如果你做到这一点它实际上将抛出一个错误。我有一个附加的,其实访问,那么一对夫妇后台线程Outlook对象模型(system.timers.timer,后台线程),我还没有在我的日志见过这样的错误。然后突然前几天,有大约10分钟的在我的插件的错误日志充满了以下错误的跨度:




应用程序调用被编组为不同的线程的接口。 (异常来自HRESULT:0x8001010E(RPC_E_WRONG_THREAD))




这10分钟跨度后,错误神秘地消失了,而我还没有作出两个错误之前立即和错误之后立即任何修改代码。在此之前,我一直没有看到任何这样的错误运行插件几个月我的机器上(主要是在缓存连接模式,如果它事项)。



我很乐意,如果有人可以点我到微软的文档,它说不能访问后台线程对象模型。


< DIV CLASS =h2_lin>解决方案

绝对不支持辅助线程Outlook对象模型。不记得了,如果他们说什么正式的,但我个人听说从几个Outlook产品经理。



扩展MAPI(或MAPI的包装,如的赎回)是从一个辅助线程访问Outlook数据的唯一途径。


I have an Office addin (Outlook if it makes a difference) created with VSTO. The addin creates a Windows Form at startup. My Form creates several threads. I'm looking for either concrete guidance or peoples' experiences with what safely works for the following situations:

1.) A thread created by the Form needs to access the Office object model (Globals.ThisAddIn.Application)

2.) A thread created by the Form does not need to access the Office object model, but does need to update a control on the Windows Form (the Form was created by the addin or 'UI' thread as I sometimes hear it referred to)

For 1.) above I've taken the following https://msdn.microsoft.com/en-us/library/8sesy69e%28v=vs.120%29.aspx to mean that it is safe as long as you set the thread's apartment state to STA and you handle exceptions. But http://weblogs.asp.net/whaggard/all-outlook-object-model-calls-run-on-the-main-thread seems to imply that in .NET VSTO, calls to the object model from any background thread are safe as they're automatically marshalled to the main thread for you, and making the 'background' thread STA is merely for performance reasons. Is that all there is to it?

For 2.) Is there any problem with letting the 'thread' be a Task or a IsBackground thread, provided it uses the control's InvokeRequired/Invoke pattern? Or does it need to be a STA thread doing the Invoking?

Update I've seen several VSTO experts mention not to touch the Outlook object model on anything other than the main thread and that in Outlook 2013 it will actually throw an error if you do it. I have an add-in that does in fact access the Outlook object model on a couple background threads (system.timers.timer, background thread) and I hadn't seen such errors in my log. Then all of a sudden a couple days ago, there was a span of about 10 minutes where my addin's error log was full of the following errors:

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

After this 10 minute span, the errors mysteriously disappeared, and I hadn't made any changes to the code, both immediately prior to the errors and immediately after the errors. Prior to that, I'd been running the addin for months on my machine (primarily in cached connection mode, if it matters) without seeing any such errors.

I'd love if someone could point me to the Microsoft documentation where it says not to access the object model on background threads.

解决方案

Outlook Object Model on a secondary thread is definitely not supported. Don't remember if they said anything officially, but I personally heard that from several Outlook Product managers.

Extended MAPI (or MAPI wrappers such as Redemption) is the only way to access Outlook data from a secondary thread.

这篇关于VSTO与Windows窗体和工作者线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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