{"id":57,"date":"2009-09-22T09:23:17","date_gmt":"2009-09-22T14:23:17","guid":{"rendered":"http:\/\/blog.bitsofgenius.com\/?p=57"},"modified":"2009-10-01T13:15:23","modified_gmt":"2009-10-01T18:15:23","slug":"the-single-application-instance-experience","status":"publish","type":"post","link":"https:\/\/blog.bitsofgenius.com\/?p=57","title":{"rendered":"The Single Application Instance Experience"},"content":{"rendered":"<p>While enhancing the Weed Killer application, I ran into an interesting problem trying to make Weed Killer a single instance application.\u00a0 There are several ways to do this: one is to use the semi-dreaded mutex. Surprisingly, the easiest way turned out to be implementing a class in the Microsoft.VisualBasic namespace from the .NET 2.0 framework: <strong>WindowsFormsApplicationBase<\/strong>.<\/p>\n<p>But, at first, it may not seem like the easiest way.\u00a0 I had to make several attempts to get my inheriting class right.\u00a0 I ran into a lot of examples on the net which were not great fits for what I was trying to do.\u00a0 And some of the examples were right in principal, but the code didn&#8217;t work.<\/p>\n<p>The class shown <a href=\"\/CodeView\/SingleInstanceApplication_cs.html\" target=\"_blank\">here<\/a> can be used for your own code, by just replacing the form you instantiate in the startup event.\u00a0 ManagerMDI is the entry point for the application.\u00a0 Its method OpenFile() takes a single parameter: the file to open.\u00a0 It internally manages whether the file is already open in a child window, or if a new window needs to be open.<\/p>\n<p>The value of inheriting from the WindowsFormsApplicationBase base class is obvious: all of the housekeeping for determining whether an instance is running or not is taken care of for the developer.\u00a0 The two startup cases are handled easily using specific events: Startup if you&#8217;re the first, or StartupNextInstance if you&#8217;re not the first.<\/p>\n<p>One little note:\u00a0 The method SetForegroundWindow() from user32.dll is used to set focus on the application, at the end of the StartupNextInstance event.\u00a0 I also use it in the Startup event, in case the application was launched from an outside source (e.g. Windows Explorer context) though it probably isn&#8217;t needed there.\u00a0 The behavior of this method is dependent on a setting for limiting focus, which you can see in Powertoys for WinXP, etc (General \/ Focus).\u00a0 If &#8220;prevent applications from stealing focus&#8221; is enabled, the method will cause the application&#8217;s taskbar entry to flash.\u00a0 When disabled, the application will be restored and receive focus on the desktop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While enhancing the Weed Killer application, I ran into an interesting problem trying to make Weed Killer a single instance application.\u00a0 There are several ways to do this: one is to use the semi-dreaded mutex. Surprisingly, the easiest way turned out to be implementing a class in the Microsoft.VisualBasic namespace from the .NET 2.0 framework: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,16,23],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-dotnetclasslibraries","category-dotnettechniques","category-dotnetwinforms"],"_links":{"self":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":0,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitsofgenius.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}