显然,管理事件,从非托管了进程外COM服务器采购的处理,被称为回随机池中的线程,而不是主STA线程上(如我期望)。 我在回答上一个问题发现了这个Internet Explorer的自动化 。 在下面的代码, DocumentComplete
是在非UI线程烧制(因此"Event thread"
是不一样的"Main thread"
在调试输出)。 因此,我必须使用this.Invoke
显示一个消息框。 据我所知,这种行为是从非托管COM客户端,在事件从一个STA线程订阅不同的自动编组回同一个线程。
什么是从传统的COM等行为偏离背后的原因是什么? 到目前为止,我还没有发现任何引用确认这一点。
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace WinformsIE
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs ev)
{
var ie = (SHDocVw.InternetExplorer)Activator.CreateInstance(Type.GetTypeFromProgID("InternetExplorer.Application"));
ie.Visible = true;
Debug.Print("Main thread: {0}", Thread.CurrentThread.ManagedThreadId);
ie.DocumentComplete += (object browser, ref object URL) =>
{
string url = URL.ToString();
Debug.Print("Event thread: {0}", Thread.CurrentThread.ManagedThreadId);
this.Invoke(new Action(() =>
{
Debug.Print("Action thread: {0}", Thread.CurrentThread.ManagedThreadId);
var message = String.Format("Page loaded: {0}", url);
MessageBox.Show(message);
}));
};
ie.Navigate("http://www.example.com");
}
}
}