如何将当前线程标识传递给处理来自COM Interop对象的事件的辅助线程? [英] How to pass the Current Thread Identity to a worker thread handling events from a COM Interop object ?

查看:75
本文介绍了如何将当前线程标识传递给处理来自COM Interop对象的事件的辅助线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是使用"Microsoft Office Interop Word"打开Word的非常简单的代码段; 1.14,并在此word实例中打开文档时执行一些代码.

Here after is a very simple piece of code to open Word using "Microsoft Office Interop Word" 1.14 and to execute some code when a document is opened within this instance of word.

我遇到的问题是在执行事件处理程序的辅助线程上没有传递在主线程上设置的标识.

The issue I experience is that the Identity set on the Main Thread is not passed on the Worker Thread executing the event handler.

我读到代表"是指在调用者的安全权限下运行,而不是在声明者的权限下运行.因此,我认为Word是通过Interop调用的,它可能无法与我的应用程序上设置的Identity一起运行.如果正确的话. 如何强制Word以该身份运行?

I read that "delegates" run under the caller's security permissions, not the declarer's permissions. I presume therefore that Word being invoked through Interop, it does possibly not run with the Identity set on my application. If this is correct. How can I force Word to run with that Identity ?

否则,我该怎么办才能在当前用户身份下运行委托

Otherwise, what can I do to run the delegate under the current user Identity

using System;using System.Windows.Forms;using Microsoft.Office.Interop.Word;using System.Diagnostics;using System.Threading;using System.Security.Principal; namespace WordInterop{    public partial class Form1 : Form    {        private ApplicationClass objWordApplication;        public Form1()        {            InitializeComponent();             Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity("It's me"), new string[] { });                    }         private void button1_Click(object sender, EventArgs e)        {            objWordApplication = new ApplicationClass();            objWordApplication.DocumentOpen += new ApplicationEvents4_DocumentOpenEventHandler(MyDelegateOpen);             objWordApplication.Visible = true;            objWordApplication.Activate();        }         private void MyDelegateOpen(_Document Doc)        {            Debug.Print("Delegate Thread Identity= " + Thread.CurrentPrincipal.Identity.Name);        }         private void Form1_Deactivate(object sender, EventArgs e)        {            Debug.Print("Main Thread Identity= " + Thread.CurrentPrincipal.Identity.Name);        }    }}

要在表单中运行此代码,您需要在Microsoft.Office.Interop.Word上添加引用,

To run this code in a Form, you need to add a reference on Microsoft.Office.Interop.Word, open the Properties tab for that reference in Visual Studio and set "Embed Interop Types" to "False".

ValéryLetroye

Valéry Letroye

推荐答案

Valery

Hi Valery,

欢迎来到MSDN论坛.

Welcome to the MSDN Forum.

我用您的代码示例重现了您的方案.我正在尝试让其他人参与此线程,这需要一些时间,感谢您的耐心等待.

I reproduced your scenario with your code sample. I am trying to involve some other one into this thread, it will take some time, thank you for your patience.

最好的问候


这篇关于如何将当前线程标识传递给处理来自COM Interop对象的事件的辅助线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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