我有一个Async
方法可以关闭一些浏览器弹出窗口。
有时,应用程序在退出时崩溃并出现.NET Runtime
错误( mscorwks.dll ,异常代码 0xc0000005 )。我怀疑当Thread
被迫终止时会发生这种情况,因为我看不到记录的“线程退出”行。可能是引发ExtendedIeBrowser
的{{1}}内的代码吗?
您可以在下面找到源代码。
unhandled exception
internal void KeepAliveAsync()
{
PageController controller = new PageController();
controller.Initialize(siteWebBrowser);
var thread = new Thread(() =>
{
_logger.Debug("Thread start - KeepAliveAsync");
try
{
while (!Program.ShouldAbortBackgroundOperations())
{
try
{
if (controller.TryCloseTimeoutWarningPopup())
{
_logger.Info("KeepAliveAsync: Closed popup successfully");
}
Thread.Sleep(5000);
}
catch (Exception ex)
{
_logger.ErrorException("KeepAliveAsync", ex);
}
}
}
catch (Exception ex)
{
_logger.ErrorException("KeepAliveAsync", ex);
}
_logger.Debug("Thread exit - KeepAliveAsync");
});
thread.Name = "KeepAliveAsync";
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
使用此类:
PageController
这是:
public class ExtendedIeBrowser : IE
{
private IntPtr hwnd;
private static Logger _logger = LogManager.GetCurrentClassLogger();
public ExtendedIeBrowser(WebBrowser webBrowserControl) : base(webBrowserControl.ActiveXInstance, false)
{
}
public void Initialize(WebBrowser webBrowserControl)
{
hwnd = webBrowserControl.FindForm().Handle;
// Start the dialog watcher or else the browser won't be able to attach the handler.
StartDialogWatcher();
}
public override IntPtr hWnd
{
get
{
return hwnd;
}
}
protected override void Dispose(bool disposing)
{
hwnd = IntPtr.Zero;
base.Dispose(disposing);
}
}
关闭弹出窗口的方法(字面意思是网页内的div):
internal static bool ShouldAbortBackgroundOperations()
{
bool should = true;
try
{
should &= Application.OpenForms.Cast<Form>().Count(f => f.ShowInTaskbar) == 0; // at least one form is shown in the taskbar
}
catch (Exception ex)
{
_logger.ErrorException("ShouldAbortBackgroundOperations", ex);
should = false;
}
return should;
}
如果在执行Abort检查和CloseTimeoutWarningPopup()后程序被关闭怎么办?我注意到在关闭应用程序后的某个时刻, internal bool TryCloseTimeoutWarningPopup()
{
bool success = false;
try
{
Div glassPane = Browser.Div(Find.ByClass(PageConstants.ModalGlassPaneClass));
if (glassPane.Exists) // there is a popup
{
Div parent = (Div)glassPane.Parent;
if (parent.InnerHtml.Contains(PageConstants.WarningLoggingOutPrimaryKeyword)) // correct content on popup
{
WatiN.Core.Button button = parent.Divs[0].Buttons[0];
ClickOnElement(button);
success = true;
}
}
}
catch (Exception ex)
{
_logger.ErrorException("CloseTimeoutWarningPopup", ex);
throw;
}
return success;
}
行正在等待元素显示并尝试访问已处置的对象。难道该对象的内存地址不再可用,因此被拒绝了吗?
答案 0 :(得分:1)
问题是controller
尝试访问的某些控件在线程终止之前被处理,导致访问冲突异常。