【发布时间】:2012-02-15 17:57:35
【问题描述】:
我的 WinApp 表单程序有一个问题,该程序包含一个带有 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 的列表。
然后我使用读取 URL 列表的 Backgroundworker 并调用 addTab 委托方法睡眠一点,直到页面加载并继续其他 AddTab 调用。
当列表为空时,我检查 DOM 页面中是否有某个字符串然后在 Backgroundworker Complete 我关闭所有选项卡并处理它们,然后单击 button1 启动 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
}
}
【问题讨论】:
-
您实际上不太可能有错误或异常。代码很不爽,Start.Back.PerformClick() 不可能工作。并且 BGW 代码实际上都没有在工作线程上运行。为表单的 FormClosing 事件添加事件处理程序并在其上设置断点。
-
您为什么建议创建更好的代码?能告诉我如何管理它吗?我创建了BackGroundworker,然后呢?后台工作程序终止后如何重新启动它?如果您能告诉我如何编写更好的代码,我将不胜感激:)
-
检查您没有收到应用程序事件日志中保存的错误;无声的 NETFX 崩溃将在那里结束......看起来很奇怪,您的应用程序没有异常处理程序弹出窗口,也没有应用程序事件日志条目。
标签: c# .net winforms exception