Create a VB.NET app that can be run in only one instance

Date: 11 Mar 2010 Comments: 0

For some application you make want the user to be able to run only one of their instances (ex. an e-mail client). I found an easy way how it can be done.

If you are developing an VB.NET Windows Form application, first you will need to get the name of your main module and then to look for it in the collection of currently running processes. Here’s a simple example how it can be done:

Imports System.Diagnostics

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim moduleName As String = Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName

Dim procName As String = System.IO.Path.GetFileNameWithoutExtension(moduleName)

If Process.GetProcessesByName(procName).Length > 1 Then

MessageBox.Show(“Already running”)

Application.Exit()

End If

End Sub

Well, that’s all :)

Leave a Reply


Spam protection by WP Captcha-Free