[英]Console application crashes after opening a web browser form because of a threadestate exception
I am developing a console application that visualizes steam items that have been filtered by name by making an html file (I am making a steam trade bot). 我正在开发一个控制台应用程序,该应用程序通过制作html文件来可视化已按名称过滤的蒸汽物品(我正在制造蒸汽交易机器人)。
In one part of the program I call a Windows form that contains a web browser (my app is console application type) when the form load the web browser is set to navigate to google just for debuging, but i get this error . 在程序的一部分中,当窗体加载设置为将Web浏览器设置为导航到google进行调试时,我调用了一个包含Web浏览器的Windows窗体(我的应用程序是控制台应用程序类型),但出现此错误 。
My console application: 我的控制台应用程序:
[STAThread]
public override void OnMessage(string message, EChatEntryType type)
{
switch(message)
{
....
case "!show":
{
Application.EnableVisualStyles();
Application.Run(new Form1());
break;
}
}
}
and my form: 和我的表格:
private void Navigate(String address)
{
if (String.IsNullOrEmpty(address)) return;
if (address.Equals("about:blank")) return;
if (!address.StartsWith("http://") &&
!address.StartsWith("https://"))
{
address = "http://" + address;
}
try
{
webBrowser1.Navigate(new Uri(address));
}
catch (System.UriFormatException)
{
return;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Navigate("google.com");
}
I can't find a solution. 我找不到解决方案。 I tried to make a new Thread but it didnt work. 我试图制作一个新的线程,但是没有用。
Well, the error message says it: ... because the current thread is not in a single-threaded apartment. 嗯,错误消息说:... ...因为当前线程不在单线程单元中。
You already have a STAThread
attribute, but you need to apply it to the entry method of the thread, ie the method that is executed when the thread starts, and what in your case probably is the Main
method: 您已经具有STAThread
属性,但是您需要将其应用于线程的入口方法,即线程启动时执行的方法,在您的情况下可能是Main
方法:
class Program
{
[STAThread]
static void Main(string[] args)
{
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.