Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent application launch if already running [duplicate]

My app needs to check and see if it is already running when launching so it doesn't open a second time. I have a system tray icon that can make the application visible = False. Works great. However, I need to make sure the user looks at the system tray for the notifyicon if the app is already running.

Private Sub mainWindowSmall_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim p() As Process
        p = Process.GetProcessesByName("TSC Tool Box")
        If p.Count > 0 Then
            MessageBox.Show("The TSC Tool Box is already running. Check System tray!", _
            "Warning !!!", MessageBoxButtons.OK, _
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
            Me.Close()
        Else
            totalOnLoad()
        End If
    End Sub

VB.NET | winforms

like image 702
Jeremy Avatar asked Feb 23 '26 11:02

Jeremy


1 Answers

This feature is already built in to Windows Forms. Go to the project properties and click the "Single Instance Application" checkbox. There's also a StartupNextInstance event that you can handle.

like image 169
Joel Coehoorn Avatar answered Feb 25 '26 01:02

Joel Coehoorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!