c# - WinApp 窗体崩溃,没有任何错误或异常 .Net

标签 c# .net winforms exception

我的 WinApp Form 程序有问题,该程序包含带有 WebBrowser 控件 DLL (GeckoFX) 的选项卡控件。

我的应用程序在运行时关闭,没有任何异常或任何东西。它可能在几分钟后或最多 10 分钟后发生。在 visual studio 中,我看到应用程序以代码 0 终止。任何东西。

在 program.cs 中,我捕获了所有这些未处理的异常

` // Add the event handler for handling UI thread exceptions to the event.
                 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(UIThreadException);

  // Set the unhandled exception mode to force all Windows Forms errors to go through
    // our handler.
                  Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

 // Add the event handler for handling non-UI thread exceptions to the event. 
                 AppDomain.CurrentDomain.UnhandledException +=
                     new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);`

我已经在 Windows 事件记录器中检查了任何错误,但它是干净的。就像程序很好地终止一样。我不知道这是否是 Gecko DLL 错误,但我不这么认为。

我使用 httpWebRequest 下载一个包含一些 URL 的列表。

然后我使用一个 Backgroundworker 来读取 URL 列表,并调用 addTab 委托(delegate)方法 稍等片刻,直到页面加载完毕,然后继续其他 AddTab 调用。

当列表为空时,我检查 DOM 页面中是否有某个字符串然后在 Backgroundworker 中完成我关闭所有选项卡并处理它们,然后单击启动 Backgroundworker1 的 button1 .asyncall();

我的逻辑有问题吗?我也会发布代码,我需要它太长但我真的需要了解终止我的应用程序的错误在哪里。如果有人可以帮助我了解为什么它会崩溃而没有任何错误或任何错误,我将不胜感激。

private void Start_Back_Click(object sender, EventArgs e)
    {                         
        List<Links> tempList = getListFromWeb();

        if (!backgroundWorker1.IsBusy)
        { 
            backgroundWorker1.RunWorkerAsync(tempGoogle);
        } 
    }

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        { 
            List<Links> temp = (List<Links>)e.Argument; 
            foreach (Links link in temp)
            {                 
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true; return;                    
                }
                _busy.WaitOne();

                if (tabs.InvokeRequired)
                { 
                        m_addTab addTabInvoke = addTabUrl;
                       Invoke(addTabInvoke, new Object[] { link.url, link.StringToSearch }); 
                }
            } 
            Thread.Sleep(2000); 
            if (tabs.InvokeRequired)
            { 
                foreach (Browser tempBrowser in ListCurrentBrowser)
                {
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        return;
                    }
                    _busy.WaitOne();
                    Thread.Sleep(1000);
                    m_SeachTab addSearchInvoke = addTabPSearch;
                    Invoke(addSearchInvoke, tempBrowser); 
                }
            }
        }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {   //Check Stuff Error and Cancelled
            if (e.Error != null)
            {... }
            else if (e.Cancelled)
            { ....}
            else //Else remove all tab
            {  
              bool canRemove = this.TabCount >= 1;
            if (canRemove)
            { 
                WebBrowserTabPage tab = this.SelectedWebBrowserTagPage; 
                this.TabPages.Remove(tab);
                tab.Dispose();
            }
             **Start.Back.PerformClick();** //Click on button again to start another time the backgroundworker
}

最佳答案

来自 Microsoft 站点:从 .NET Framework 版本 4 开始,不会为破坏进程状态的异常引发此事件,例如堆栈溢出或访问冲突,除非事件处理程序是安全关键的并具有 HandleProcessCorruptedStateExceptionsAttribute 属性。 也许您应该尝试添加该属性。

对于 Application.ThreadException,再次来自 Microsoft 站点: “为保证不会遗漏此事件的任何激活,您必须在调用 Application.Run 之前附加一个处理程序。” 在您的代码中,不清楚您是否在调用 Application.Run 之前附加了处理程序。

此外 - 您可能希望在可能调用非托管代码的地方使用“通用”try catch block :

try {
// Code goes here
}
catch { //Unmanaged exceptions will be caught here as well.

}

try { 
// Code goes here.
}
catch(Exception ex){ // only managed exceptions are caught, avoid that in your case.
}

第一个会捕获非托管异常,而第二个则不会。

关于c# - WinApp 窗体崩溃,没有任何错误或异常 .Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8953774/

相关文章:

c# - 在虚拟模式下在 ListView 中分组

c# - 使用 gacutil 安装 .dll

c# - 添加结构 : Interface to a List<Interface>

c# - 我应该学习什么来提高我的技能?

c# - 如何读取特定名称的所有 XML 节点并将它们放入数组(或列表)中?

c# - Graphics.RotateTransform() 在绘制图像后仍然存在

c# - HtmlAgilityPack 根据查询过滤 HTML

c# - 如何使用 linq 通过entity.ids 查找匹配实体的最大日期

c# - 在 WinForms 输入焦点上自动弹出平板电脑触摸键盘

c# - 一个如此简单的 Winform 程序——但我可以恢复内存吗?