获得在C#里面插入面板网页浏览器错误 [英] Getting error on inserting webbrowser inside panel in c#
问题描述
我正在开发中,我需要插入panl.while内的一个网页浏览器做desginer码这里面我收到错误的应用程序无法获取web浏览器控制的窗口句柄。窗口的ActiveX控件不支持为此,我已经把代码后我得到的错误来运行它里面STSthread.but十字线。
i am developing an application in which i require to insert a webbrowser inside a panl.while doing this i am getting error inside desginer code "Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported."for which i have put the code to run it inside STSthread.but after that i am getting error of "cross thread.."
Thread newThread = new Thread(newThreadStart(MethodToCallCOMMethod));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
private void MethodToCallCOMMethod()
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.webBrowser2 = new System.Windows.Forms.WebBrowser();
if (this.InvokeRequired)
{
this.BeginInvoke(new MethodInvoker(delegate
{
this.p_bottom.Controls.Add(this.webBrowser2);
}));
}
else
{
this.p_bottom.Controls.Add(this.webBrowser2);
}
}
使用invokerequired后,我仍然得到跨线程错误: - 控制从另一个线程,它是用C#创建的访问?,怎么解决呢..请帮助我。
after using invokerequired i am still getting "cross thread error:-control'' access from another thread that it was created in c#",how to solve it..?please help me.
推荐答案
问题是,你在不同的线程创建化网页浏览器,一般你不应该GUI线程之外创建或编辑控件。
The problem is that you are creating the WebBrowsers in a different thread, in general you should never create or edit controls outside the gui thread.
因此,无论创建webBrowser1和2个其他地方(在GUI线程),或者把MethodInvoker委托里面的代码(运行在GUI线程)
So either create the webBrowser1 and 2 somewhere else (in the GUI Thread) or put the code inside the MethodInvoker delegate (which runs in the GUI Thread)
如果它说当前线程不在单线程单元
那么无论你的主要功能并不像这样(东西VS默认创建),或试图从另一个线程创建它们:
If it says the current thread is not in a single-threaded apartment
then either your Main function doesn't look like this (what VS creates by default) or you tried to create them from another thread:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
这篇关于获得在C#里面插入面板网页浏览器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!