您的位置:首页 > 其它

引用呵呵,正宗的,一字不差,多线程 WEBBROWSER

2009-04-16 13:49 309 查看
原贴志址:http://www.zhangsichu.com/blogview.asp?Content_Id=79

在非UI 线程上Show一个含有WebBrowser的Form出现的问题


字体大小 [ ]
问题描述:
客户端的Windows程序使用WebMethod从服务器上取得一个系统信息列表。信息列表中有多条Message。当系统消息的时间合要求,使用一个自定义的MessageForm Show出这个系统Message。MessageForm是一个含有WebBrowser的WinForm。 Show MessageForm的调用在一个Timer中被执行。当调用ShowMessage的操作时会出现下面的错误。



出错的代码:

private void btnTestS_Click(object sender, EventArgs e)
{
DataRow row = (new DataTable()).NewRow();
System.Threading.Timer timer = new System.Threading.Timer(
new System.Threading.TimerCallback(ShowMessageFormS),
row,
0,
30000);
}
private void ShowMessageFormS(object status)
{
(new MessageForm()).Show();
}
在new MessageForm时,MessageForm调用自己的InitializeComponent()方法时出错。如果把MessageForm上的WebBrowser控件删除掉,程序可以正常Show出Form,不出错。

问题调查:
从程序的错误信息看出,可能是ActiveX的套间问题。ActiveX的WebBrowser要求当前Thread是一个single-thread apartment。WebBrowser Com组件要求当前线程是一个单套间的,而System.Threading.Timer起来的线程是一个MTAThread 多套间的。产生了问题。

修改方案:
修改方案一: 使用一个新线程启动MessageForm,设置这个线程的ApartmentState为STA

private void btnTestT_Click(object sender, EventArgs e)
{
DataRow row = (new DataTable()).NewRow();
System.Threading.Timer timer = new System.Threading.Timer(
new System.Threading.TimerCallback(ShowMessageFormT),
row,
0,
300000);
}

private void ShowMessageFormT(object status)
{
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ParameterizedThreadStart(ShowMessage));
thread.SetApartmentState(System.Threading.ApartmentState.STA);
thread.Start(status);
}

private void ShowMessage(object status)
{
System.Windows.Forms.Application.Run(new MessageForm());
(new MessageForm()).ShowDialog();
}
修改方案二:找到主UI线程,用主UI线程调度,Show MessageForm

private void btnTestG_Click(object sender, EventArgs e)
{
DataRow row = (new DataTable()).NewRow();
System.Threading.Timer timer = new System.Threading.Timer(
new System.Threading.TimerCallback(ShowMessageFormG),
row,
0,
300000);
}
private delegate void ShowMessageHandler(DataRow row);
private void ShowMessageFormG(object status)
{
if (System.Windows.Forms.Application.OpenForms[0].InvokeRequired)
{
System.Windows.Forms.Application.OpenForms[0].Invoke(new ShowMessageHandler(ShowMessageFormG), new object[] { status });
return;
}
ShowMessage(status);
}
private void ShowMessage(object status)
{
(new MessageForm()).ShowDialog();
}
示例中的DataRow row = (new DataTable()).NewRow(); 没有实际意义,在实际代码中ShowMessage需要一个DataRow来绘制消息。示例代码中的DataRow只是为了模拟实际的环境,没有实际意义。

再添加一个方法的

public partial class Form1 : Form
{

private void button1_Click(object sender, EventArgs e)
{
System.Threading.Thread s = new System.Threading.Thread(new System.Threading.ThreadStart(test));
s.ApartmentState = System.Threading.ApartmentState.STA;
s.Start();
}

public void test()
{
this.Invoke(new EventHandler(this.invokeTest));
}
public void invokeTest(object sender,EventArgs e)
{
System.Windows.Forms.WebBrowser wb = new WebBrowser();
this.Controls.Add(wb);
}
public Form1()
{
InitializeComponent();
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: