Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « VB.Net: Using PostSharp and Log4PostSharpMeeting up with Oren Eini aka Ayende Rahien »
    comments

    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

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    10 comments

    Comment from: Alex Ullrich [Member] Email
    ****-
    Alex Ullrich 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.
    08/14/08 @ 07:56
    Comment from: chip1216 [Member] Email
    ****-
    chip1216 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.....
    01/23/09 @ 09:16
    Comment from: chip1216 [Member] Email
    ***--
    chip1216 Could I get a complete zip of the project?
    01/23/09 @ 09:17
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) I'll see what I can do this weekend.

    Be patient.
    01/23/09 @ 12:30
    Comment from: Christiaan Baes (chrissie1) [Member]
    Christiaan Baes (chrissie1) The weekend just got a whole lot shorter.
    01/23/09 @ 13:06
    Comment from: Chip1216 [Visitor]
    ****-
    Chip1216 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.
    01/28/09 @ 08:59
    Comment from: Adil Kara [Visitor]
    *****
    Adil Kara Thank u very much
    I searched Single App instance for vb.net, i have very code but this is bette :)

    Thanks bro
    04/15/09 @ 00:20
    Comment from: Max [Visitor] Email
    Max Thank you.
    Although if i need to use application.restart it won't work, how should i go about fixing this?
    12/30/09 @ 16:33
    Comment from: Max [Visitor]
    Max Figured it out.
    I added:
    Private Shared _isRestarting As Boolean = False
    Public Shared Property IsRestarting() As Boolean
    Get
    Return _isRestarting
    End Get
    Set(ByVal value As Boolean)
    _isRestarting = value
    End Set
    End Property

    in the singleInstanceController and this in the this_StartupNextInstance method:
    If IsRestarting = True Then
    OnCreateMainForm()
    Else
    Me.MainForm.Show()
    Me.MainForm.BringToFront()
    End If

    and before i called app.restart i set the property to true, hope it helps someone.

    Cheers
    01/01/10 @ 10:41
    Maxim I have more elegant solution: http://blogs.microsoft.co.il/blogs/maxim/archive/2010/02/13/single-instance-application-manager.aspx
    02/19/10 @ 02:58

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

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