入门WebBrowser控件工作的控制台应用程序?控制台、控件、应用程序、入门

由网友(憂藍·似殘夢)分享简介:我有一个能够通过web浏览器对象打印HTML的打印机类。我希望能够从一个控制台应用程序打印,但我得到一个错误,当我的打印机类尝试创建一个web浏览器对象:I have a printer class that is capable of printing HTML via the WebBrowser object....

我有一个能够通过web浏览器对象打印HTML的打印机类。我希望能够从一个控制台应用程序打印,但我得到一个错误,当我的打印机类尝试创建一个web浏览器对象:

I have a printer class that is capable of printing HTML via the WebBrowser object. I want to be able to print from a console application, but I get an error when my printer class tries to create a WebBrowser object:

WebBrowser browser = new WebBrowser();

错误:

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot 
be instantiated because the current thread is not in a 
single-threaded apartment.  

我尝试添加引用System.Windows.Forms的到我的控制台应用程序,但没有奏效。我没有什么是怎么回事丝毫的想法,但我想AP preciate的帮助。

I tried adding a reference to System.Windows.Forms into my console application but that didn't work. I don't have the slightest idea of what's going on here, but I would appreciate the help.

推荐答案

添加STAThread属性的主要方法。

Add STAThread attribute to your main method.

[STAThread]
public static Main()
{
    ...
}

更新:在这里,你应该用线,在创建浏览器做

Update: Here what you should do with thread where Browser is created

thread.SetApartmentState(ApartmentState.STA);

更新2:

如果每个应用程序的一个线程:

If one thread per app:

Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
阅读全文

相关推荐

最新文章