首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WinApp窗体崩溃,没有任何错误或异常.Net

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

Stack Overflow用户
提问于 2012-01-21 22:34:50
回答 3查看 5.7K关注 0票数 13

我的WinApp表单程序有一个问题,它包含一个带有WebBrowser控件动态链接库(GeckoFX)的选项卡控件。

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

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

代码语言:javascript
复制
` // 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,并调用addTab委托方法休眠一段时间,直到页面加载完毕,然后继续其他AddTab调用。

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

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

代码语言:javascript
复制
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
}

}

EN

回答 3

Stack Overflow用户

发布于 2012-01-31 22:06:41

来自Microsoft站点:从.NET框架版本4开始,除非事件处理程序是安全关键的并且具有属性,否则不会为损坏进程状态的异常引发此事件。也许你应该试着添加这个属性。

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

此外,您可能希望在可能调用非托管代码的位置上使用“通用”try catch块:

代码语言:javascript
复制
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.
}

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

票数 1
EN

Stack Overflow用户

发布于 2012-01-24 20:31:14

实际上,当另一个线程发生未处理的异常时,整个进程就会终止。您需要在Debug/ Exceptions /Common Language Runtime Exceptions这两个复选框都设置的情况下运行应用程序。

票数 0
EN

Stack Overflow用户

发布于 2012-01-31 18:31:44

尝试在backgroundWorker1_DoWork中的代码周围放置一个try/catch块,并在catch子句中放置一个断点,您应该能够捕获异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8953774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档