Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET Runtime exception caused by thread unhandled exception
    primarykey
    data
    text
    <p>I have an <code>Async</code> method that closes some browser popups.</p> <p>Some times the application crashes upon exit with a <code>.NET Runtime</code> error (<strong>mscorwks.dll</strong> with exception code <strong>0xc0000005</strong>). I suspect it happends when the <code>Thread</code> is forced to terminate, because I can't see the logged "Thread exit" line. Could it be the code inside the <code>ExtendedIeBrowser</code> that throws an <code>unhandled exception</code>?</p> <p>You can find the source code below.</p> <pre><code>internal void KeepAliveAsync() { PageController controller = new PageController(); controller.Initialize(siteWebBrowser); var thread = new Thread(() =&gt; { _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(); } </code></pre> <p>The <code>PageController</code> uses this class:</p> <pre><code>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); } } </code></pre> <p>And this is:</p> <pre><code> internal static bool ShouldAbortBackgroundOperations() { bool should = true; try { should &amp;= Application.OpenForms.Cast&lt;Form&gt;().Count(f =&gt; f.ShowInTaskbar) == 0; // at least one form is shown in the taskbar } catch (Exception ex) { _logger.ErrorException("ShouldAbortBackgroundOperations", ex); should = false; } return should; } </code></pre> <p>The method that closes the popup(literally it's a div inside a web page):</p> <pre><code> 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; } </code></pre> <p>What if the program is closed after the Abort checking and CloseTimeoutWarningPopup() is executed? I noticed that at some point after I closed the app, the <code>if (glassPane)</code> row was waiting for the element to show up and tried to access a disposed object. Could it be that object's memory address no longer available and therefore denied?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload