title: How can I catch an application crash/ungraceful exit?
body: <p>I have a VB.NET WinForms application running from an executable stored on a network share. In that application, I have defined the <code>UnhandledException</code> handler in the <code>ApplicationEvents</code> (<code>Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException</code>). In my handler, I have a method that logs the exception details to a text file before prompting the user to confirm exiting the application.</p>
  <p>However, this application &quot;randomly&quot; crashes and exits completely without any log created or message box displayed. This behavior happens at different executable points in the application and I'm trying desperately to track down the cause. I'm <em>guessing</em> that the problem may be due to either a temporary loss of network connectivity or some other issue communicating with the PostgreSQL database, but I can't confirm the source since there's no stack trace or message detail provided before the application disappears from the user's screen.</p>
  <p>This should be &quot;simple&quot;, but I'm at a loss as I've tried several things, including wrapping massive blocks of code in <code>Try...Catch</code> blocks and adding additional logging features to my error handler. I've tried rearranging the code in my <code>UnhandledException</code> handler to avoid any issues with new object instantiation (for my <code>ErrorHandler</code> object). I added a check in the error handling for logging the error locally if the network is unavailable. I've even added a simple message box to the <code>FormClosing</code> event of my main form if the closing wasn't directly initiated by the user to try to at least have the application do <em>something</em> before shutting down completely.</p>
  <p>No matter what I've tried so far, the application still forcibly exits during seemingly random times. The user will be pressing a button to execute any of a number of methods that usually work normally. If the user relaunches the application after being kicked out and performs the exact same action again, it works without a problem. What I need to accomplish is some form of &quot;idiot-proofing&quot; the error handling so that whatever is causing the application's exit is caught and logged. I'm sure there are things I'm not thinking of at this point, so let me know if any further clarification is needed.</p>
  <hr />
  <h2>CODE</h2>
  <p>The application's <code>Startup</code> event handler:</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
      Try
          Common.ApplicationStartup(ApplicationSettings.CurrentUser)
      Catch ex As Exception
          Dim StartupException As New ErrorHandler(ex)
  
          StartupException.LogException()
          MessageBox.Show(&quot;You do not have permission to access this resource.&quot; &amp; vbCrLf &amp; vbCrLf &amp;
                      &quot;The application will now exit.&quot;)
          System.Environment.Exit(1)
      End Try
  
      ' *********************************************************************
      ' ** Notify the user if the application is running in test mode.     **
      ' *********************************************************************
      If ApplicationSettings.TestMode Then
          MessageBox.Show(&quot;This application is currently running in Test Mode, and will use &quot; &amp;
                          &quot;local paths for data and configuration information.&quot; &amp; vbCrLf &amp; vbCrLf &amp;
                          &quot;If you are trying to use this application with live data and see &quot; &amp;
                          &quot;this message, please contact the IT HelpDesk for assistance.&quot;, &quot;TEST MODE&quot;,
                          MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  
          If ApplicationSettings.CurrentUser.Department = Users.Employee.Department.IS Then
              If MessageBox.Show(&quot;Do you want to continue in Test Mode?&quot;, &quot;TEST MODE&quot;, MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.No Then
                  ApplicationSettings.TestMode = False
              End If
          End If
      End If
  
      ' *********************************************************************
      ' ** Initialize any application-specific settings here.              **
      ' *********************************************************************
      Try
          'If ApplicationSettings.TestMode AndAlso ApplicationSettings.CurrentUser.Department = Users.Employee.Department.IS Then
          '    MessageBox.Show(&quot;If you have any additional parameters/settings to configure for this application, &quot; &amp;
          '                    &quot;please do so before commenting out this message.&quot;,
          '                    &quot;DEVELOPMENT WARNING&quot;, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
          'End If
      Catch ex As Exception
          Dim ExHandling As New Common.ErrorHandler(ex)
  
          ExHandling.LogException()
  
          MessageBox.Show(&quot;There was a problem with initializing the application's configuration.&quot; &amp; vbCrLf &amp; vbCrLf &amp;
                      &quot;The application will now exit.&quot;)
          System.Environment.Exit(2)
      End Try
  End Sub
  </code></pre>
  <p>The <code>ApplicationStartup</code> method:</p>
  <pre class="lang-vb prettyprint-override"><code>Public Sub ApplicationStartup(ByRef CurrentUser As Users.Employee)
      ' *********************************************************************
      ' ** Default the TestMode variable to False.  If the check for       **
      ' ** whether or not the application is running from the IDE fails,   **
      ' ** the application should assume that it's running live.           **
      ' *********************************************************************
      ApplicationSettings.TestMode = False
  
      ' *********************************************************************
      ' ** Perform a check of whether or not the application is running    **
      ' ** from the IDE or the Debug folder.                               **
      ' *********************************************************************
      SetTestMode()
  
      ' *********************************************************************
      ' ** Retrieve any parameters sent to the executable from the command **
      ' ** line and determine if the application is running from the task  **
      ' ** scheduler.                                                      **
      ' *********************************************************************
      ApplicationSettings.ScheduledTask = False
      ApplicationSettings.RuntimeParameters = System.Environment.GetCommandLineArgs().ToList
  
      If Not ApplicationSettings.RuntimeParameters Is Nothing AndAlso ApplicationSettings.RuntimeParameters.Count &gt; 0 Then
          For Each Parameter As String In ApplicationSettings.RuntimeParameters
              If Parameter.ToUpper.Contains(&quot;SCHEDTASK&quot;) Then
                  ApplicationSettings.ScheduledTask = True
                  Exit For
              End If
          Next
      End If
  
      ' *********************************************************************
      ' ** Set up the CurrentUser object by querying Active Directory and  **
      ' ** the PostgreSQL database for details.                            **
      ' *********************************************************************
      Try
          If CurrentUser.ADUserName Is Nothing OrElse String.IsNullOrEmpty(CurrentUser.ADUserName) Then
              CurrentUser = New Users.Employee(Environment.UserName)
          End If
      Catch UserEx As Exception
          Dim ExHandler As New ErrorHandler(UserEx)
  
          ExHandler.LogException()
          Throw UserEx
      End Try
  
      If CurrentUser Is Nothing Then
          Throw New Exception(&quot;Username &quot; &amp; Environment.UserName &amp; &quot; was not found in Active Directory.&quot;)
      ElseIf CurrentUser.Enabled = False Then
          Throw New Exception(&quot;Username &quot; &amp; Environment.UserName &amp; &quot; is not a currently active employee.&quot;)
      End If
  
      ' *********************************************************************
      ' ** Default the DBCommandTimeout variable to 30.                    **
      ' *********************************************************************
      ApplicationSettings.DBCommandTimeout = 30
  End Sub
  
  Private Sub SetTestMode()
      ' *********************************************************************
      ' ** Use the Debug.Assert to call the InTestMode function, which     **
      ' ** will set the TestMode variable to True.  Debug.Assert will only **
      ' ** execute if the program is running from a debugging version of   **
      ' ** the code (in Design-Time, or from the Debug folder).  When the  **
      ' ** code is running from a compiled executable, the Debug.Assert    **
      ' ** statement will be ignored.                                      **
      ' *********************************************************************
      Debug.Assert(InTestMode)
  End Sub
  
  Private Function InTestMode() As Boolean
      ' *********************************************************************
      ' ** Set the global TestMode variable to True.  This function is     **
      ' ** only called in debug mode using the Debug.Assert method in the  **
      ' ** SetTestMode Sub.  It will not be called if the application is   **
      ' ** running from a compiled executable.                             **
      ' *********************************************************************
      Common.ApplicationSettings.TestMode = True
      Return True
  End Function
  </code></pre>
  <p>The <code>UnhandledException</code> event handler:</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
      Dim Response As DialogResult = DialogResult.Yes
  
      Response = MessageBox.Show(&quot;An unknown error occurred in the application.&quot; &amp; vbCrLf &amp; vbCrLf &amp;
                                 &quot;Do you want to exit the application?&quot;, &quot;UNHANDLED EXCEPTION&quot;,
                                 MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
  
      Dim UnhandledError As New ErrorHandler(e.Exception)
  
      UnhandledError.LogException()
  
      If Response = DialogResult.Yes Then
          e.ExitApplication = True
      Else
          e.ExitApplication = False
      End If
  End Sub
  </code></pre>
  <p>The main form's <code>FormClosing</code> event:</p>
  <pre class="lang-vb prettyprint-override"><code>Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      If Not e.CloseReason = CloseReason.UserClosing Then
          MessageBox.Show(&quot;The application has encountered some sort of problem and is closing.&quot;)
      End If
  End Sub
  </code></pre>
  <p>Let me know if you want/need to see more code. As I said, the error occurs at seemingly random points in the application's execution and are inconsistent between one try or another.</p>
  <hr />
  <h2>UPDATE 7/1/2020</h2>
  <hr />
  <p>I haven't come back to this topic for a while because I moved a copy of the executable (and the supporting libraries) to the user's local drive and had her run the application from there. While she was using that copy, she wasn't &quot;booted&quot; from the program as described above (she had a few errors here and there, but those were all handled by my exception handling routine as expected).</p>
  <p>Here we are a few months later and I've had cause to switch the user back to using the copy of the executable from the network share. I just received a report from the user that she's once again experiencing the issue with being randomly kicked out of the application without any warning or error and I'm not getting any of my exception &quot;reports&quot;. Luckily for me, she's done a decent job of documenting the occurrences.</p>
  <p>The strange thing is that sometimes these crashes occur when she's not doing anything &quot;special&quot;. A couple of times they've happened when she simply clicks on one of the toolstrip menus to display a drop-down list of submenus. I've checked and there isn't any event handling code for these parent toolstrip menus, so it isn't like there are any queries or other instructions being executed. It should simply be displaying the submenu.</p>
  <p>FWIW, a couple of weeks ago, we had a serious connectivity issue between our office and the server on which these executables are being stored (hosted VM's accessed via site-to-site VPN). I was getting around 10% packet loss across the VPN, even though I didn't see any packet loss anywhere else. I never found out what was causing the packet loss, but it appears to have been resolved and I can only assume that one of the ISP's between here and there had a faulty piece of equipment that they repaired/replaced. When I run a PING test to the server across the VPN, I'm not seeing any significant packet loss (maybe 1 packet out of several thousand) and response times of 15-35ms.</p>
  <p>At this point, I'm only guessing (obviously), but I'm thinking that perhaps there's some sort of &quot;time-out&quot; occurring on the VPN connection that's causing a loss of connection to the code base. It's a total pS.W.A.G. (<em>(pseudo) Scientific Wild-@$$ Guess</em>), but I'm trying to come up with a viable solution to address the issue.</p>
  <hr />
  <h3>MY IDEAS</h3>
  <p><strong>One thought is this:</strong> All of my in-house applications are run from this server and all of the supporting libraries for each are stored in the executable folder. Yes, this means that I've got multiple copies of many libraries stored on the server in various folders. I've been <em>wanting</em> to reduce this duplication, but I haven't really had/taken the time to figure out the best way to do so. At this point, I'm considering some sort of &quot;installer&quot; package for each of the workstations to drop the necessary libraries into each user's GAC (<em>Global Assembly Cache</em>) instead of accessing them through the VPN.</p>
  <p>The only problem with this (that I can think of) is that there are several legacy systems that use different versions of the same libraries. For example, my current development is using Npgsql v4.1.3.1, but there are some applications that are still using v2.x and I don't really have time to go through every application to find which ones are/aren't using the current version <em>and</em> implement a version upgrade. That's just one of the many libraries where such an issue would arise, so I suppose I'd need to try to install <em>all</em> of the in-use versions to each GAC.</p>
  <p><strong>Another thought:</strong> Bring all of the executables back to a local server (not over the VPN) and change all of the shortcuts to point to that version instead of the one that requires the VPN. This, obviously, would have the benefit of less dependency on things like Internet connectivity and 3rd-party systems, as well as reduced latency.</p>
  <p>The issue with <em>this</em> option, however, is that it's completely &quot;unsupported&quot; by my bosses. Their response when I've suggested something similar in the past is along the lines of &quot;we're paying for a hosted server and <em>they</em> should support it...&quot; Well, we all know that something like this may well be beyond the scope of any reasonable support request for a 3rd-party server host.</p>
  <p>I'm really leaning towards the GAC option - at least as a first step - but I'll need to do a bit of research before I start traipsing down that road. Does anyone else have other suggestions for ways I might be able to deal with this? I'm really running out of ideas and I've got to find a real, workable and sustainable solution.</p>
  <h2>MORE INFO</h2>
  <hr />
  <p>I've implemented the suggestion below from @djv of wrapping the application's launch in a &quot;startup&quot; form that starts a new thread, but that still hasn't been able to catch whatever is causing the crash. The application still just periodically dies with absolutely no logging that I've been able to find so far.</p>
  <p>I've also included, in the <code>ApplicationEvents</code>, a very simple handler for the <code>NetworkAvailabilityChanged</code> event to try to catch something happening there.</p>
  <pre class="lang-vb prettyprint-override"><code>        Private Sub MyApplication_NetworkAvailabilityChanged(sender As Object, e As NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
              If Not My.Computer.Network.IsAvailable Then
                  MessageBox.Show(&quot;Network connection has been lost.&quot;, &quot;NETWORK CONNECTION TESTING&quot;, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
              End If
          End Sub
  </code></pre>
  <p>Unfortunately, even <em>that</em> hasn't given me any additional insights as the user hasn't ever seen that <code>MessageBox</code>.</p>
  <p>I <em>have</em> found an error in the user's Windows Event Logs that seems to correspond to the most recent event, but I'm not sure what it means, exactly:</p>
  <pre class="lang-none prettyprint-override"><code>EVENT ID 1000
  ---
  Faulting application name: &lt;EXECUTABLE_NAME&gt;.exe, version: 1.0.0.0, time stamp: 0x9d491d36
  Faulting module name: clr.dll, version: 4.8.4180.0, time stamp: 0x5e7d1ed7
  Exception code: 0xc0000006
  Fault offset: 0x000cc756
  Faulting process id: 0xe570
  Faulting application start time: 0x01d64fc245d7d922
  Faulting application path: \\&lt;SERVER_NAME&gt;\&lt;SHARE_PATH&gt;\&lt;EXECUTABLE_NAME&gt;.exe
  Faulting module path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
  Report Id: 7016e3cc-7406-4854-95be-dbe3231447e7
  Faulting package full name: 
  Faulting package-relative application ID: 
  </code></pre>
  <p>This seems to indicate something in the CLR crapping out, but that really doesn't seem to give me any more information than I had before.</p>
  <h2>FURTHER DIAGNOSTICS/RESEARCH FOR SPECIFIC ISSUE</h2>
  <hr />
  <p>After digging around in the Event Logs some more, I found a couple more errors from around the time of the aforementioned event:</p>
  <pre class="lang-none prettyprint-override"><code>EVENT ID 1005
  ---
  Windows cannot access the file  for one of the following reasons: there is a problem with the network connection, the disk that the file is stored on, or the storage drivers installed on this computer; or the disk is missing. Windows closed the program &lt;EXECUTABLE_NAME&gt; because of this error.
  
  Program: &lt;EXECUTABLE_NAME&gt;
  File: 
  
  The error value is listed in the Additional Data section.
  User Action
  1. Open the file again. This situation might be a temporary problem that corrects itself when the program runs again.
  2. If the file still cannot be accessed and
      - It is on the network, your network administrator should verify that there is not a problem with the network and that the server can be contacted.
      - It is on a removable disk, for example, a floppy disk or CD-ROM, verify that the disk is fully inserted into the computer.
  3. Check and repair the file system by running CHKDSK. To run CHKDSK, click Start, click Run, type CMD, and then click OK. At the command prompt, type CHKDSK /F, and then press ENTER.
  4. If the problem persists, restore the file from a backup copy.
  5. Determine whether other files on the same disk can be opened. If not, the disk might be damaged. If it is a hard disk, contact your administrator or computer hardware vendor for further assistance.
  
  Additional Data
  Error value: C00000C4
  Disk type: 0
  </code></pre>
  <p>And this:</p>
  <pre class="lang-none prettyprint-override"><code>EVENT ID 1026
  ---
  Application: &lt;EXECUTABLE_NAME&gt;.exe
  Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.Runtime.InteropServices.SEHException
     at &lt;ROOT_NAMESPACE&gt;.frmPayments.get_instance()
     at &lt;ROOT_NAMESPACE&gt;.frmMain.tsmiProcessPayments_Click(System.Object, System.EventArgs)
     at System.Windows.Forms.ToolStripItem.RaiseEvent(System.Object, System.EventArgs)
     at System.Windows.Forms.ToolStripMenuItem.OnClick(System.EventArgs)
     at System.Windows.Forms.ToolStripItem.HandleClick(System.EventArgs)
     at System.Windows.Forms.ToolStripItem.HandleMouseUp(System.Windows.Forms.MouseEventArgs)
     at System.Windows.Forms.ToolStripItem.FireEventInteractive(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
     at System.Windows.Forms.ToolStripItem.FireEvent(System.EventArgs, System.Windows.Forms.ToolStripItemEventType)
     at System.Windows.Forms.ToolStrip.OnMouseUp(System.Windows.Forms.MouseEventArgs)
     at System.Windows.Forms.ToolStripDropDown.OnMouseUp(System.Windows.Forms.MouseEventArgs)
     at System.Windows.Forms.Control.WmMouseUp(System.Windows.Forms.Message ByRef, System.Windows.Forms.MouseButtons, Int32)
     at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.ToolStrip.WndProc(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.ToolStripDropDown.WndProc(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.Control+ControlNativeWindow.OnMessage(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
     at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
     at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
     at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
     at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
     at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
     at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
     at &lt;ROOT_NAMESPACE&gt;.StartupForm.Main()
     at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
     at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  <p>Doing some further research on the error code and exception information, it would seem that the problems are, in fact, due to the executable being loaded from the network share across the VPN.</p>
  <p>Some of the information I found:</p>
  <ul>
  <li><a href="https://stackoverflow.com/a/20050572/2569697">Unable to start applications from network “0xc0000006”
  </a></li>
  <li><a href="https://stackoverflow.com/a/8310791/2569697">Performance penalties for .NET app running from shared network folder</a></li>
  <li><a href="https://stackoverflow.com/q/13588057/2569697">How does IMAGE_FILE_NET_RUN_FROM_SWAP in an EXE file affect runtime libraries</a></li>
  </ul>
  <p>This last one had me doing some research on PE file options and how to set them for a VB.NET application, but I wasn't finding enough to feel like pursuing that line of thought would provide enough benefit.</p>
  <p>I guess this means that I need to do something to bring everything back across the VPN for execution. Perhaps I'll do some sort of actual local installation for the application (now I need to figure out how to actually do that, but that's well beyond the scope of this question). I'm not particularly happy about it, but at least I have an idea of what direction to take this from here.</p>
  <h2>BACK TO THE ORIGINAL QUESTION</h2>
  <hr />
  <p>However, this <em>still</em> doesn't answer my original question about how to catch and handle these app-killer exceptions. I'd be &quot;fine&quot; with the application crashing if it had <em>at least</em> given me some indication as to what happened at the time. I thought that the <code>UnhandledException</code> handler in the <code>ApplicationEvents</code> would catch even these, but further research into the <code>SEHException</code> at least helps me to get an idea of why it doesn't - see <a href="https://stackoverflow.com/a/16437000/2569697">SEHException not caught by Try/Catch</a>. I know it'd be a nightmare to try to define rules to handle or ignore every single type of exception that could possibly crop up in the <code>UnhandledException</code> event handler, but it would've been a whole lot nicer and more helpful for troubleshooting to at least see <em>something</em>.</p>