{"id":284,"date":"2011-10-27T16:19:04","date_gmt":"2011-10-27T14:19:04","guid":{"rendered":"http:\/\/dev.flauschig.ch\/wordpress\/?p=284"},"modified":"2011-11-23T10:10:16","modified_gmt":"2011-11-23T09:10:16","slug":"suppress-the-application-has-stopped-working-dialog","status":"publish","type":"post","link":"http:\/\/dev.flauschig.ch\/wordpress\/?p=284","title":{"rendered":"Suppress the &#8220;[Application] has stopped working&#8221; Dialog"},"content":{"rendered":"<p>If you&#8217;re working with Windows, your definitely familiar with the sight of the &#8220;[Application] has stopped working&#8221; &#8482; dialog<\/p>\n<p><a class=\"lightview\" href=\"http:\/\/dev.flauschig.ch\/wordpress\/wp-content\/uploads\/2011\/10\/error-reporting.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-285\" title=\"error-reporting\" src=\"http:\/\/dev.flauschig.ch\/wordpress\/wp-content\/uploads\/2011\/10\/error-reporting-300x161.png\" alt=\"\" width=\"300\" height=\"161\" srcset=\"http:\/\/dev.flauschig.ch\/wordpress\/wp-content\/uploads\/2011\/10\/error-reporting-300x161.png 300w, http:\/\/dev.flauschig.ch\/wordpress\/wp-content\/uploads\/2011\/10\/error-reporting.png 532w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Whether that is good or bad in general? I don&#8217;t care. Not today at least. I am much more interested in a particular use case where this is most certainly not what you want. That case is if you run another application as a sub-process using the System.Diagnostics.Process class. Maybe you&#8217;re running other applications that simply crash occasionally and you want to automatically restart them, maybe you just don&#8217;t want the users to know. In any case, if the sub-process pops up with this dialog it doesn&#8217;t terminate the actual process until one of the buttons is clicked which is absolutely not what I wanted in my use case.<!--more--><\/p>\n<p>So here&#8217;s how to solve that: There is the Win32 API function SetErrorMode (http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms680621%28v=vs.85%29.aspx) that allows you to suppress different types of error dialogs, one of them being the &#8220;[Application] has stopped working&#8221; dialog. The error mode you set using this method is automatically inherited by child processes which is exactly what I needed. Since directly working with Win32 calls is always kind of icky, I created a nice little wrapper for this functionality. It consists of the class ErrorModeContext and the enum ErrorModes<\/p>\n<pre class=\"brush: csharp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\npublic class ErrorModeContext : IDisposable\r\n{\r\n  private readonly int _oldMode;\r\n\r\n  public ErrorModeContext(ErrorModes mode)\r\n  {\r\n    _oldMode = SetErrorMode((int)mode);\r\n  }\r\n\r\n  ~ErrorModeContext()\r\n  {\r\n    Dispose(false);\r\n  }\r\n\r\n  private void Dispose(bool disposing)\r\n  {\r\n    SetErrorMode(_oldMode);\r\n  }\r\n\r\n  public void Dispose()\r\n  {\r\n    Dispose(true);\r\n    GC.SuppressFinalize(this);\r\n  }\r\n\r\n  &#x5B;DllImport(&quot;kernel32.dll&quot;)]\r\n  private static extern int SetErrorMode(int newMode);\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"brush: csharp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n&#x5B;Flags]\r\npublic enum ErrorModes\r\n{\r\n  Default = 0x0,\r\n  FailCriticalErrors = 0x1,\r\n  NoGpFaultErrorBox = 0x2, \/\/ &amp;lt;- this is the one we need\r\n  NoAlignmentFaultExcept = 0x4,\r\n  NoOpenFileErrorBox = 0x8000,\r\n}\r\n<\/pre>\n<p>I have implemented the ErrorModeContext as an IDisposable so that I can use it in a using block with the auto-cleanup feature. I just place the code that runs my child process inside a using block and the problem disappears.<\/p>\n<pre class=\"brush: csharp; title: ; wrap-lines: false; notranslate\" title=\"\">\r\nusing (new ErrorModeContext(ErrorModes.FailCriticalErrors | ErrorModes.NoGpFaultErrorBox))\r\n{\r\n\/\/ start child process\r\n\/\/ and wait for it to finish\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re working with Windows, your definitely familiar with the sight of the &#8220;[Application] has stopped working&#8221; &#8482; dialog Whether that is good or bad in general? I don&#8217;t care. Not today at least. I am much more interested in a particular use case where this is most certainly not what you want. That case &hellip; <\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[4],"tags":[118,117],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-executor","4":"has-more-link","5":"post-284","7":"format-standard","8":"category-csharp","9":"post_tag-application-has-stopped-working","10":"post_tag-jit"},"acf":[],"views":9105,"_links":{"self":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/284"}],"collection":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=284"}],"version-history":[{"count":0,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/284\/revisions"}],"wp:attachment":[{"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=284"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dev.flauschig.ch\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}