简体   繁体   English

由于线程异常,打开Web浏览器表单后,控制台应用程序崩溃

[英]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.

相关问题 ASP.Net应用程序因Web服务而崩溃 - ASP.Net Application Crashes because of Web Service 在表单应用程序中使用控制台窗口时发生异常 - Exception when using console window in a form application 打开时表单崩溃 - Form crashes when opening 间歇性地打开SaveDialog会使应用程序崩溃 - Opening the SaveDialog intermittently crashes application 控制台应用程序在 visual studio 中打开后立即关闭 - Console application closes immediately after opening in visual studio visual studio在控制台应用程序而不是Web项目中打开Web浏览器+调试器 - visual studio opening web browser + debugger in a console app rather than a web project Xamarin 应用程序在实现 MasterDetailPage 后无任何异常崩溃 - Xamarin Application crashes without any exception after implementing MasterDetailPage 在C#控制台应用程序中的Web浏览器中打开pdf - Open pdf in web browser in C# console application 忽略控制台应用程序中的Web浏览器SSL安全警报 - Ignoring web browser SSL security alerts in console application 在控制台应用程序中创建表单后如何隐藏控制台 - How to hide console after creating form in console application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM