Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

July 2009
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

XML Feeds

Authors

« VB.Net: Using PostSharp and Log4PostSharpMeeting up with Oren Eini aka Ayende Rahien »
The Desktop Developers Journal

VB.Net: Single Instance application the better way

by chrissie1


Permalink 14 Aug 2008 04:31 , Categories: Microsoft Technologies, VB.NET Tags: application, instance, single, singleton, vb.net

Based on Scott Hanselman’ s blogpost named The Weekly Source Code 31- Single Instance WinForms and Microsoft.VisualBasic.dll

We all know about this little setting, right? At least now you do ;-).

For you C# lovers don’t search for it, it’s only there in VB.Net.

And this little nasty side effect.

That meant you had to have all your startupcode in the constructor or loadevent of that form (:'()

So Scott gave me the inspiration to do this.

First of all go back to the properties of your project and uncheck the “enable application framework” thing and never check it again :>. Then we can just have a sub main in a module.

  1. Module StartupModule
  2.  
  3. #Region " Public methods "
  4.     ‘'’ <summary>
  5.     ‘'’ Beginning of the application
  6.     ‘'’ </summary>
  7.     ‘'’ <remarks></remarks>
  8.     Public Sub Main()
  9.         Dim _StartUp As New Startup.StartUp
  10.         _StartUp.Start()
  11.     End Sub
  12. #End Region
  13.  
  14. End Module

And this is the Startup class

  1. Namespace Startup
  2.     ‘'’ <summary>
  3.     ‘'’
  4.     ‘'’ </summary>
  5.     ‘'’ <remarks></remarks>
  6.     Public Class StartUp
  7.         Implements Interfaces.IStartUp
  8.  
  9. #Region " private members "
  10.         ‘'’ <summary>
  11.         ‘'’
  12.         ‘'’ </summary>
  13.         ‘'’ <remarks></remarks>
  14.         Private _Log As TDB2007.Utils.Logging.Logging.Interfaces.ILog
  15. #End Region
  16.  
  17. #Region " Constructors "
  18.         ‘'’ <summary>
  19.         ‘'’  
  20.         ‘'’ </summary>
  21.         ‘'’ <remarks></remarks>
  22.         Public Sub New()
  23.  
  24.         End Sub
  25. #End Region
  26.  
  27. #Region " public method "
  28.         ‘'’ <summary>
  29.         ‘'’ The start method for eassier unit testing
  30.         ‘'’ </summary>
  31.         ‘'’ <remarks></remarks>
  32.         Public Sub Start() Implements Interfaces.IStartUp.Start
  33.             CommonFunctions.IoC.IoCUtil.InsertConfigOfContainingType(Of View.Menu.IoC.ConfigureStructureMap)()
  34.             log4net.Config.XmlConfigurator.Configure()
  35.             Application.EnableVisualStyles()
  36.             Dim controller As View.Menu.Startup.Interfaces.ISingleInstanceController = CommonFunctions.IoC.Container.Resolve(Of View.Menu.Startup.Interfaces.ISingleInstanceController)()
  37.             CommonFunctions.IoC.Container.Resolve(Of Controller.Interfaces.IStart).Start()
  38.             Try
  39.                 controller.Run(Environment.GetCommandLineArgs())
  40.             Catch Ex As Exception
  41.                 _Log = CommonFunctions.IoC.Container.Resolve(Of Utils.Logging.Logging.Interfaces.ILog)()
  42.                 _Log.LogWarning("Unhandled Exception - " & Ex.Message)
  43.                 _Log.LogWarning("Stacktrace : " & Ex.StackTrace)
  44.             End Try
  45.         End Sub
  46. #End Region
  47.  
  48.     End Class
  49. End Namespace

Which calls our singleinstancecontroller.

  1. Imports Microsoft.VisualBasic.ApplicationServices
  2.  
  3. Namespace Startup
  4.     ‘'’ <summary>
  5.     ‘'’
  6.     ‘'’ </summary>
  7.     ‘'’ <remarks></remarks>
  8.     Public Class SingleInstanceController
  9.         Inherits WindowsFormsApplicationBase
  10.         Implements Interfaces.ISingleInstanceController
  11.  
  12. #Region " Constructors "
  13.         ‘'’ <summary>
  14.         ‘'’
  15.         ‘'’ </summary>
  16.         ‘'’ <remarks></remarks>
  17.         Public Sub New()
  18.             IsSingleInstance = True
  19.         End Sub
  20. #End Region
  21.  
  22. #Region " Evenhandlers "
  23.         ‘'’ <summary>
  24.         ‘'’ When the user tries to open our application again then menu will just become visible and be pushed to the front.
  25.         ‘'’ </summary>
  26.         ‘'’ <param name="sender"></param>
  27.         ‘'’ <param name="e"></param>
  28.         ‘'’ <remarks></remarks>
  29.         Private Sub this_StartupNextInstance(ByVal sender As Object, ByVal e As StartupNextInstanceEventArgs) Handles MyBase.StartupNextInstance
  30.             Me.MainForm.Show()
  31.             Me.MainForm.BringToFront()
  32.         End Sub
  33. #End Region
  34.  
  35. #Region " public methods "
  36.         ‘'’ <summary>
  37.         ‘'’ When the application is run the first time this method is called.
  38.         ‘'’ </summary>
  39.         ‘'’ <remarks>We make a new instance of the Menu and it will be shown</remarks>
  40.         Protected Overrides Sub OnCreateMainForm()
  41.             MainForm = CType(CommonFunctions.IoC.Container.Resolve(Of Forms.Interfaces.IMenu)(), System.Windows.Forms.Form)
  42.         End Sub
  43.  
  44.         ‘'’ <summary>
  45.         ‘'’
  46.         ‘'’ </summary>
  47.         ‘'’ <param name="args"></param>
  48.         ‘'’ <remarks></remarks>
  49.         Public Shadows Sub Run(ByVal args As String()) Implements Interfaces.ISingleInstanceController.Run
  50.             MyBase.Run(args)
  51.         End Sub
  52. #End Region
  53.  
  54.     End Class
  55. End Namespace

A working project/solution to download.

SingleInstance.zip

7 comments »Send a trackback » 2970 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

7 comments

Comment from: AlexCuse [Member] Email
****-
This is interesting. I've always just used a mutex for things like this, but I like the way that this (from the looks of it) will bring the app's menu to the front.
14/08/08 @ 07:56
Comment from: chip1216 [Member] Email
****-
This is what I need to do, but can't figure out how to cut and paste this into my VB project. Get all kinds of compile errors. The _log stuff I could remove, but other stuff is still a problem such as Error 2 Type 'Interfaces.ISingleInstanceController' is not defined. D:\VB\start\start\startup.vb 9 20 start
AND
Error 13 Name 'CommonFunctions' is not declared. D:\VB\start\start\startup.vb 90 13 start
I'd like to get a complete project.....
23/01/09 @ 09:16
Comment from: chip1216 [Member] Email
***--
Could I get a complete zip of the project?
23/01/09 @ 09:17
Comment from: chrissie1 [Member] Email
I'll see what I can do this weekend.

Be patient.
23/01/09 @ 12:30
Comment from: chrissie1 [Member] Email
The weekend just got a whole lot shorter.
23/01/09 @ 13:06
Comment from: Chip1216 [Visitor]
****-
Only one more small question.... When the application is called the second & subsequent times, how/where would I retrieve any command line argument(s)?

I assume this has to happen in this_StartupNextInstance.

Sorry if I sound stupid, but this kind of VB stuff is all new to me.
28/01/09 @ 08:59
Comment from: Adil Kara [Visitor]
*****
Thank u very much
I searched Single App instance for vb.net, i have very code but this is bette :)

Thanks bro
15/04/09 @ 00:20

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)