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

Tags: application

comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

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 imageChristiaan is a forensic technician who programs on the side, although my function description says that I do IT-things for 90% of the time . I'm an avid VB.NET fan and I use lots of the ALT.Net techniques, like unit-testing, nhibernate, logging, IoC, ...
Social SitingsTwitterLinkedInHomePageLTD RSS Feed
4425 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback