<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">My 00000010 cents</title><subtitle type="html">Ponderings of a Runtime Dev</subtitle><id>http://blogs.msdn.com/tyler_whitney/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/tyler_whitney/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2005-11-23T23:40:00Z</updated><entry><title>What reference on VB does someone who writes VB use?</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2007/01/11/what-reference-on-vb-does-someone-who-writes-vb-use.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2007/01/11/what-reference-on-vb-does-someone-who-writes-vb-use.aspx</id><published>2007-01-11T08:44:00Z</published><updated>2007-01-11T08:44:00Z</updated><content type="html">&lt;P&gt;Do you ever wonder what reference a person who works on the VB product uses?&amp;nbsp; For myself, I love the work Francesco Balena did in his &lt;EM&gt;Programming Microsoft Visual Basic.NET&lt;/EM&gt; book.&amp;nbsp; I got to meet Francesco while I was in Italy doing a VB tour.&amp;nbsp; I got the 2005 edition of his book (Programming Microsoft Visual Basic 2005:The Language) which is also fantastic.&amp;nbsp; The breadth and depth of his books make them one-stop shopping for me, in many cases.&lt;/P&gt;
&lt;P&gt;I also like to use Paul Vick's&lt;EM&gt; The Visual Basic .NET Programming Language.&amp;nbsp; &lt;/EM&gt;Paul wrote our language spec and&amp;nbsp;his book is another one that I keep in close reach.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1449290" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author></entry><entry><title>More on extending the My Namespace</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2006/12/06/more-on-extending-the-my-namespace.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2006/12/06/more-on-extending-the-my-namespace.aspx</id><published>2006-12-06T21:23:00Z</published><updated>2006-12-06T21:23:00Z</updated><content type="html">&lt;P&gt;I wrote an&amp;nbsp;&lt;SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 'Calibri','sans-serif'; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;A class="" href="http://msdn.microsoft.com/coding4fun/hardware/robotics/article.aspx?articleid=1115750" target=_blank mce_href="http://msdn.microsoft.com/coding4fun/hardware/robotics/article.aspx?articleid=1115750"&gt;&lt;FONT color=#800080&gt;article&lt;/FONT&gt;&lt;/A&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;for Coding4Fun on extending the My namespace so that you can program the LEGO Mindstorm robots.&amp;nbsp; Whether you are actually interested in programming LEGO robots with the My namespace or not (it is actually pretty fun :-) it is another example of how you can go about extending the My namespace.&amp;nbsp; &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1224713" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author></entry><entry><title>Splash Screens in VB 2005</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2006/02/01/522291.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2006/02/01/522291.aspx</id><published>2006-02-01T21:09:00Z</published><updated>2006-02-01T21:09:00Z</updated><content type="html">&lt;p&gt;&lt;font face=Verdana size=2&gt;One of the things we provided in the application model for VB is the ability to easily integrate a splash screen into your application.&amp;nbsp; We take care of displaying it, making sure it displays for at least some user specifiable period of time, and taking it down once your application's main form is ready to display.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;One of the things that throws people is how to set the minimum amount of time for the splash screen to display.&amp;nbsp; Admittedly, we have some work to do to make this easier.&amp;nbsp; &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;For now, if you don't like the default two second minimum, you can specify your own minimum in milliseconds by overriding the OnInitialize method on the MyApplication class.&amp;nbsp; Here's how you do it (and note that all of this presupposes that you have the application model enabled in the first place):&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;Go to the project designer by double-clicking the 'My Project' node in your solution.&amp;nbsp; Once you've specified which form is to be your splash screen, click &lt;/font&gt;&lt;font face=Verdana size=2&gt;the View Application Events button.&amp;nbsp; You'll see a MyApplication class inside the My namespace.&amp;nbsp; Go in that class and add an override to OnInitiliaze like this:&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face="Courier New" size=2&gt;Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean&lt;br/&gt;&amp;nbsp; Me.MinimumSplashScreenDisplayTime = 5000&lt;br/&gt;&amp;nbsp; Return MyBase.OnInitialize(commandLineArgs)&lt;br/&gt;End Function&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;Now the splash screen will stay up a minimum of five seconds.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;The splash screen displays early on in your applications startup.&amp;nbsp; The idea is to have that displaying until your main form is ready to display.&amp;nbsp; Then we take it down unless you've specified a minimum 'face time' that you want for your splash screen.&amp;nbsp; Once the minimum splash time expires we take the splash screen down.&amp;nbsp; Just set it to zero if you want it taken down automatically once your main form has completed loading.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font face=Verdana size=2&gt;All of this exposes you to a variety of things going on behind the scenes.&amp;nbsp; Next time I'll write about the application model and what the sequence of events is when an app that uses the new application model starts up.&lt;/font&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=522291" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author></entry><entry><title>Single-Instance Console Apps</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2006/01/12/512290.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2006/01/12/512290.aspx</id><published>2006-01-13T01:40:00Z</published><updated>2006-01-13T01:40:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Arial&gt;A question was posted on a MSDN VB forum about how to get single-instance behavior in a Whidbey app when your startup object isn't a form.&amp;nbsp; The IDE application designer doesn't accommodate this scenario.&amp;nbsp; But you can get single-instance behavior in situations like this by writing against the application model base class yourself.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Here's an example of a VB8 (Whidbey) console application that exhibits single-instance behavior:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;Option Explicit On&lt;BR&gt;Option Strict On&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;Module Module1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub Main(ByVal args() As String)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Dim App As New Application&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; App.Run(args) 'passing args in enables App.CommandLineArgs&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR&gt;End Module&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;Class Application&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inherits Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Sub New()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'If you are setting your own custom principal instead of using&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'the windows principal, pass AuthenticationMode.ApplicationDefined&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'so we don't run it over in the ctor.&amp;nbsp; The default ctor puts the &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'windows principal on the main thread.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyBase.New()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Me.IsSingleInstance = True 'Turns on the single-instance behavior&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Protected Overrides Sub OnRun()'Put your code that you'd&amp;nbsp;have place in Main(), here.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("Running as " &amp;amp; My.User.Name &amp;amp; ". Press a key to exit")&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;While Not System.Console.KeyAvailable&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DoEvents() 'pumps messages so StartupNextInstance() gets called&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End While&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, _&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) _&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Handles Me.StartupNextInstance&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MsgBox("An attempt to launch an additional instance was made.")&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; End Sub&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;End Class&lt;BR&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face=Arial&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;When&amp;nbsp;you first run this app you'll get the message that it is running and it will just hang out waiting for a key press.&amp;nbsp; If you&amp;nbsp;launch it again while the first instance is still running, the first instance will display the dialog about an attempt to launch another instance.&amp;nbsp; The subsequent attempt to launch will just return without launching a new instance.&amp;nbsp; You now have a single-instance console application.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;I am utilizing the single-instance behavior logic which lives in the Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase class.&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Arial&gt;Note that in the ctor for Application that I call MyBase.New() without any arguments.&amp;nbsp; This will result in the windows principal being put on the main thread.&amp;nbsp; My.User will then reflect the windows account that is running this app.&amp;nbsp; If you are setting your own custom principal, pass in AuthenticationMode.ApplicationDefined instead so that your principal doesn't get run over when the class initializes.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;I&amp;nbsp;use Main() simply to create my application object and start it up.&amp;nbsp; I put my startup logic, i.e. the code that would traditionally be in Main(), inside Application.OnRun().&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;You'll notice that I'm pumping events in OnRun().&amp;nbsp; Pumping events is only necessary if you want to use the application model events, such as StartupNextInstance().&amp;nbsp; WindowsApplicationBase uses the message pump to marshal events onto the main thread.&amp;nbsp; So if you aren't yielding control to windows you'll need to pump messages in your app if you want the app model events.&amp;nbsp; If you don't care about handling the application model events then you can safely ignore that part.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;Note that you also get access to the other application model events such as the Shutdown event.&amp;nbsp; You won't get the UnhandledException() event because it isn't getting hooked up.&amp;nbsp; Getting that to work is a subject for another post someday if there is interest in that.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Arial&gt;By the way, if you are curious you can see the code generated by the IDE for Windows Forms apps.&amp;nbsp; Just create a windows forms app, turn on single-instance in the application page of the project designer, turn on 'view all files' in the solution explorer, and then look at the hidden file &lt;EM&gt;Application.Designer.vb&lt;/EM&gt; which is under the&amp;nbsp;MyProject/Application.MyApp node.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=512290" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author></entry><entry><title>Single-Instance Applications and C#</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2005/11/28/497604.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2005/11/28/497604.aspx</id><published>2005-11-28T23:12:00Z</published><updated>2005-11-28T23:12:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In response to a question about C# and single-instance apps I thought I'd show how to create a single-instance app in C# using the VB app model class.&amp;nbsp; The VB app model class does the heavy lifting for a number of things, including single-instance behavior.&amp;nbsp; The model was designed by the VB team and consequently has the VB customer primarily in mind.&amp;nbsp; So the VB project designer does a lot of the leg work for you if you are building a VB windows forms app and makes it easy to configure, handle the app model events, etc.&amp;nbsp; C# doesn't natively support the application model but that doesn't mean you can't use C# to enjoy the benefits--it just means you have to do a bunch of stuff manually.&amp;nbsp; Here's a bare bones single-instance C# application.&amp;nbsp; I started with a new C# Windows forms app and replaced the contents of program.cs with the following (note that you'll also need a reference to Microsoft.VisualBasic.dll):&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;using System;&lt;BR&gt;using System.Collections.Generic;&lt;BR&gt;using System.Windows.Forms;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;namespace WindowsApplication1&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; static class Program&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// The main entry point for the application.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [STAThread]&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; static void Main(string[] commandLine)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Application.SetCompatibleTextRenderingDefault(false);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; App myApp = new App();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; myApp.Run(commandLine);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///&amp;nbsp; We inherit from WindowsFormApplicationBase which contains the logic for the application model, including&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///&amp;nbsp; the single-instance functionality.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; class App : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public App()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.IsSingleInstance = true; // makes this a single-instance app&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.EnableVisualStyles = true; // C# windowsForms apps typically turn this on.&amp;nbsp; We'll do the same thing here.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses; // the vb app model supports two different shutdown styles.&amp;nbsp; We'll use this one for the sample.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// This is how the application model learns what the main form is&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected override void OnCreateMainForm() &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; this.MainForm = new Form1();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// Gets called when subsequent application launches occur.&amp;nbsp; The subsequent app launch will result in this function getting called&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// and then the subsequent instances will just exit.&amp;nbsp; You might use this method to open the requested doc, or whatever &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;/summary&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; /// &amp;lt;param name="eventArgs"&amp;gt;&amp;lt;/param&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; base.OnStartupNextInstance(eventArgs);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Windows.Forms.MessageBox.Show("An attempt to launch another instance of this app was made");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;In the code sample above I could have handled the myApp.StartupNextInstance event instead of overriding OnStartupNextInstance().&amp;nbsp; No biggy either way, I just went for the easy override here.&amp;nbsp; Whether you override or handle the event you'll get called on the main thread.&amp;nbsp; If you do override OnStartupNextInstance() you'll want to defer to the base class as in addition to firing the StartupNextInstance event it also sets the focus to your original instance.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;When you launch this app the first time you'll see&amp;nbsp;Form1.&amp;nbsp; If you launch it again while the first instance is running you'll see the message about a subsequent instance being launched as OnStartupNextInstance() gets called.&amp;nbsp; The subsequent instance will just quietly exit while the original instance keeps running.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;You can get a feel for other things you can do with the app model by looking at how VB configures it.&amp;nbsp; Create a new VB windows app and go to the project designer via the solution explorer by double-clicking the "My Project" node.&amp;nbsp; If you show all files in the solution explorer, you can also examine the Application.Designer.VB file which is under the My Project / Application.myapp node.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana size=2&gt;Legal blather: Use of included script samples are subject to the terms specified at &lt;/FONT&gt;&lt;A href="http://www.microsoft.com/info/cpyright.htm"&gt;&lt;FONT face=Verdana size=2&gt;http://www.microsoft.com/info/cpyright.htm&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=497604" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author><category term="Application Model" scheme="http://blogs.msdn.com/tyler_whitney/archive/tags/Application+Model/default.aspx" /></entry><entry><title>Single-Instance Applications</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/tyler_whitney/archive/2005/11/23/VB-Application-Model.aspx" /><id>http://blogs.msdn.com/tyler_whitney/archive/2005/11/23/VB-Application-Model.aspx</id><published>2005-11-24T02:40:00Z</published><updated>2005-11-24T02:40:00Z</updated><content type="html">&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;So what's a Single-Instance application?&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Imagine launching an application where additional attempts to launch the app while the first one is running doesn't actually launch a new instance of the app.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Instead the original instance of the app gets notified that another launch attempt occurred.&amp;nbsp; When we&amp;nbsp;introduced an application model for Visual Basic in VB 2005, one of the things&amp;nbsp;we wanted to do is make it drop-dead easy to create single-instance applications.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;?&lt;o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;It's easier to understand single-instance apps with an example.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I don't know for certain that Word is a single-instance app but it seems to behave like one.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If you crank up the task manager and launch Word you'll see WinWord.exe show up.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;But if you launch it again while an instance of Word is already running, you won't see another WinWord.exe show up in the task manager.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You can see the memory footprint of Word go up but it only goes up a little.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;I assume that is memory needed just for the new document.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;How Word manages its single-instance behavior is frankly not something I know anything about so I'm only guessing at what it is doing based on watching the task manager.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;But it has the tale-tale signs of a single-instance app.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;Single-Instance apps are useful in situations where you don't want to have multiple instances of the app running.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Maybe you can only have one running on the machine at any given time.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Or maybe you have a scenario where you have an app that can manage multiple documents.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In that case a single-instance app would provide a nice way to avoid loading multiple copies of your exe just to manage one more document.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Instead, the currently running app could just be told to load another document. Less memory consumption, faster subsequent document load times.&amp;nbsp; It's goodness.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;You can find code samples on the internet to create single-instance apps.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Previously you had to hunt down the code, wire it into your own startup code, and then decide whether it used a technique that you think is robust enough or secure enough for your purposes.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;We decided we wanted to provide a robust/secure single-instance capability that you could just turn on by clicking on a checkbox.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This switch lives on the application page in the project designer under Windows application framework properties and is called:&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Make single instance application.&amp;nbsp; That's a lot of intro for the issue I want to talk about.''&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;One of the questions that comes up is: &lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;So how do we determine whether a subsequent instance of an application has launched?&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Do we key off of the path where the app was launched from, or what?&lt;/SPAN&gt;&lt;u1:p&gt;&lt;/u1:p&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;We don't key off of the path where the app was launched from.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Single-Instance apps have a single-instance application identity.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;When a single-instance app launches, it checks to see if an app with the same identity is already running.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If it is, we call that app's StartupNextInstance() event (which will be raised on the main thread of the original instance) and pass it the command line.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The subsequent instance then just exits; leaving the original instance to decide what it wants to do about the fact that somebody tried to launch another instance of itself.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;A common thing to do would be to examine the command line passed into the StartupNextInstance() event and try to load the document specified. &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;u1:p&gt;&lt;/u1:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;STRONG&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;What does this single-instance application identity look like?&lt;/SPAN&gt;&lt;/STRONG&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It is comprised of the assembly guid and the assembly (not file) major and minor version number.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You can see the assembly guid and assembly version information by selecting the Assembly Information button on the application page in the project designer.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Taken together these three pieces of information identify the identity of the single-instance application.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;u1:p&gt;&lt;/u1:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;There are some ramifications from all of this.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Different versions of an application won't act as single instances against each other.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;For instance, if you launch version 1.0 of Foo.exe, and then launch version 1.1 of Foo.exe, you won't get the single instance behavior.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;You'll have two versions of foo.exe running.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;u1:p&gt;&lt;/u1:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;If you have two copies of foo.exe on the machine in c:\foo1 and c:\foo2, and then launch c:\foo1\foo.exe followed by c:\foo2\foo.exe, you will get single-instance behavior between the two.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;That is, you'll only get one instance of foo.exe running, the one in c:\foo1\foo.exe.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Launching c:\foo2\foo.exe will simply raise the event on the first and then terminate.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;The application startup directory is not part of the single-instance application identity so it has no bearing on whether an application thinks another instance of itself is already running.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;u1:p&gt;&lt;/u1:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;This means that you can't easily contrive a situation where c:\foo1\foo.exe only exhibits single instance behavior when foo.exe is launched from c:\foo1 instead of c:\foo2 merely because they are in different directories.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If you wanted c:\foo2\foo.exe to be considered a different app then c:\foo1\foo.exe, you'd have to differentiate the two foo.exes by either changing the assembly version number or assembly guid of one of them.&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;u1:p&gt;&lt;/u1:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-FAMILY: 'Arial Narrow'; mso-bidi-font-family: Arial"&gt;&lt;u1:p&gt;&lt;/u1:p&gt;Currently there is no user facing way of setting the single-instance identity.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;In practice do people find that this is terribly limiting?&lt;/SPAN&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=496505" width="1" height="1"&gt;</content><author><name>Tyler Whitney</name><uri>http://blogs.msdn.com/members/Tyler+Whitney.aspx</uri></author></entry></feed>