Nachdem ich bei Klaus gelesen hatte, wie er es gelöst hat, eine App nur einmal zu starten, musste ich nochmal extrem nachdenken, bei welchem Projekt und wie ich das gelöst hatte.

Das mir das nicht nochmal passiert, habe ich hier mal den Code verewigt.

Ursprünglich von Codeproject in c#.

    1 Imports System

    2 Imports System.Windows.Forms

    3 Imports System.Runtime.InteropServices

    4 Imports System.Text

    5 Imports System.Diagnostics

    6 Imports System.Threading

    7 Imports System.Reflection

    8 Imports System.IO

    9 

   10 Namespace SingleInstance

   11     ''' <summary>

   12     ''' Summary description for SingleApp.

   13     ''' </summary>

   14     Public Class SingleApplication

   15 

   16         Public Sub New()

   17         End Sub

   18         ''' <summary>

   19         ''' Imports

   20         ''' </summary>

   21 

   22         <DllImport("user32.dll")> _

   23         Private Shared Function ShowWindow(ByVal hWnd As IntPtr, _

   24                                            ByVal nCmdShow As Integer) As Integer

   25         End Function

   26 

   27         <DllImport("user32.dll")> _

   28         Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Integer

   29         End Function

   30 

   31         <DllImport("user32.dll")> _

   32         Private Shared Function IsIconic(ByVal hWnd As IntPtr) As Integer

   33         End Function

   34 

   35         ''' <summary>

   36         ''' GetCurrentInstanceWindowHandle

   37         ''' </summary>

   38         ''' <returns></returns>

   39         Private Shared Function GetCurrentInstanceWindowHandle() As IntPtr

   40             Dim hWnd As IntPtr = IntPtr.Zero

   41             Dim process As Process = Process.GetCurrentProcess()

   42             Dim processes As Process() = Process.GetProcessesByName(process.ProcessName)

   43             For Each _process As Process In processes

   44                 ' Get the first instance that is not this instance, has the

   45                 ' same process name and was started from the same file name

   46                 ' and location. Also check that the process has a valid

   47                 ' window handle in this session to filter out other user's

   48                 ' processes.

   49                 If _process.Id <> process.Id _

   50                     AndAlso _process.MainModule.FileName _

   51                         = process.MainModule.FileName _

   52                     AndAlso _process.MainWindowHandle _

   53                         <> IntPtr.Zero Then

   54                     hWnd = _process.MainWindowHandle

   55                     Exit For

   56                 End If

   57             Next

   58             Return hWnd

   59         End Function

   60         ''' <summary>

   61         ''' SwitchToCurrentInstance

   62         ''' </summary>

   63         Private Shared Sub SwitchToCurrentInstance()

   64             Dim hWnd As IntPtr = GetCurrentInstanceWindowHandle()

   65             If hWnd <> IntPtr.Zero Then

   66                 ' Restore window if minimised. Do not restore if already in

   67                 ' normal or maximised window state, since we don't want to

   68                 ' change the current state of the window.

   69                 If IsIconic(hWnd) <> 0 Then

   70                     ShowWindow(hWnd, SW_RESTORE)

   71                 End If

   72 

   73                 ' Set foreground window.

   74                 SetForegroundWindow(hWnd)

   75             End If

   76         End Sub

   77 

   78         ''' <summary>

   79         ''' Execute a form base application if another instance already running on

   80         ''' the system activate previous one

   81         ''' </summary>

   82         ''' <param name="frmMain">main form</param>

   83         ''' <returns>true if no previous instance is running</returns>

   84         Public Shared Function Run(ByVal frmMain As System.Windows.Forms.Form) As Boolean

   85             If IsAlreadyRunning() Then

   86                 'set focus on previously running app

   87                 SwitchToCurrentInstance()

   88                 Return False

   89             End If

   90             Application.Run(frmMain)

   91             Return True

   92         End Function

   93 

   94         ''' <summary>

   95         ''' for console base application

   96         ''' </summary>

   97         ''' <returns></returns>

   98         Public Shared Function Run() As Boolean

   99             If IsAlreadyRunning() Then

  100                 Return False

  101             End If

  102             Return True

  103         End Function

  104 

  105         ''' <summary>

  106         ''' check if given exe alread running or not

  107         ''' </summary>

  108         ''' <returns>returns true if already running</returns>

  109         Private Shared Function IsAlreadyRunning() As Boolean

  110             Dim strLoc As String = Assembly.GetExecutingAssembly().Location

  111             Dim fileInfo As FileSystemInfo = New FileInfo(strLoc)

  112             Dim sExeName As String = fileInfo.Name

  113             Dim bCreatedNew As Boolean

  114 

  115             mutex = New Mutex(True, "Global\" + sExeName, bCreatedNew)

  116             If bCreatedNew Then

  117                 mutex.ReleaseMutex()

  118             End If

  119 

  120             Return Not bCreatedNew

  121         End Function

  122 

  123         Shared mutex As Mutex

  124         Const SW_RESTORE As Integer = 9

  125     End Class

  126 End Namespace

  127 

  128