WinApp窗体崩溃,没有任何错误或异常.Net
本文关键字:错误 异常 Net 任何 窗体 崩溃 WinApp | 更新日期: 2023-09-27 18:20:54
我的WinApp Form程序有问题,该程序包含带有WebBrowser控件DLL(GeckoFX)的选项卡控件。
我的应用程序在运行时关闭,没有任何异常。它可能在几分钟后发生,也可能在10分钟后发生。在visual studio中,我看到应用程序终止,代码为0。任何东西
在程序.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事件记录器中检查了任何错误,但它是干净的。就像节目结束得很好一样。我不知道这是否是壁虎DLL的错误,但我不这么认为。
我使用httpWebRequest下载一个包含一些URL的列表。
然后,我使用Backgroundworker
读取URL列表,并调用addTab Delegate Method Sleep一点,直到页面加载,然后继续其他addTab invoke。
当列表为空时,我检查DOM页面中是否有特定字符串。然后在Backgroundworker
Complete中,我关闭所有选项卡并处理它们,然后单击按钮1,启动Backgroundworker1.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 Site:从.NET Framework版本4开始,除非事件处理程序是安全关键的并且具有HandleProcessCorruptedStateExceptionsAttribute属性,否则不会针对损坏进程状态的异常(如堆栈溢出或访问冲突)引发此事件也许你应该尝试添加这个属性。
对于Application.ThreadException,请再次从Microsoft网站:"为了保证不会错过此事件的激活,您必须在调用Application.Run之前附加一个处理程序。"在您的代码中,不清楚您是否在调用Application.Run.之前附加了处理程序
此外,您可能希望在可能调用非托管代码的地方使用"通用"try-catch块:
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.
}
第一个将捕获非托管异常,而第二个则不会。
实际上,当另一个线程中发生未处理的异常时,整个进程都会终止。您需要在调试下运行应用程序,同时设置调试/异常/公共语言运行时异常的两个复选框。
试着在backgroundWorker1_DoWork中的代码周围放一个Try/catch块,并在catch子句中放一个断点,你应该能够捕获异常。