Home
Manage Your Code
Snippet: Single Instance Application (C#)
Title: Single Instance Application Language: C#
Description: This provides an easy way to make a single instance application in C#. Simply put this code in your application, and whenever you would call Application.Run, call SingleInstanceApp.Run. Views: 342
Author: Anonymous Anonymous Date Added: 1/26/2008
Copy Code  
1using System;
2using System.Diagnostics;
3using System.Drawing;
4using System.Drawing.Imaging;
5using System.Runtime.InteropServices;
6using System.Windows.Forms;
7using System.Threading;
8
9internal static class SingleInstanceApp{
10
11    [DllImport( "user32.dll" )]
12    private static extern int ShowWindow( IntPtr hWnd, int nCmdShow );
13
14    [DllImport( "user32.dll" )]
15    private static extern int SetForegroundWindow( IntPtr hWnd );
16
17    [DllImport( "user32.dll" )]
18    private static extern int IsIconic( IntPtr hWnd );
19
20    private static IntPtr GetCurrentInstanceWindowHandle() {
21        IntPtr hWnd = IntPtr.Zero;
22        Process process = Process.GetCurrentProcess();
23        Process[] processes = Process.GetProcessesByName( process.ProcessName );
24        foreach ( Process _process in processes ) {
25            // Get the first instance that is not this instance, has the

26            // same process name and was started from the same file name

27            // and location. Also check that the process has a valid

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

29            // processes.

30            if ( _process.Id != process.Id &&
31                _process.MainModule.FileName == process.MainModule.FileName &&
32                _process.MainWindowHandle != IntPtr.Zero ) {
33                hWnd = _process.MainWindowHandle;
34                break;
35            }
36        }
37        return hWnd;
38    }
39
40    private static void SwitchToCurrentInstance() {
41        IntPtr hWnd = GetCurrentInstanceWindowHandle();
42        if ( hWnd != IntPtr.Zero ) {
43            if ( IsIconic( hWnd ) != 0 ) {
44                ShowWindow( hWnd, SW_RESTORE );
45            }
46
47            SetForegroundWindow( hWnd );
48        }
49    }
50
51    public static bool Run( System.Windows.Forms.Form frmMain ) {
52        if ( IsAlreadyRunning() ) {
53            SwitchToCurrentInstance();
54            return false;
55        }
56        Application.Run( frmMain );
57        return true;
58    }
59
60    private static bool IsAlreadyRunning() {
61        string strLoc = System.Reflection.Assembly.GetExecutingAssembly().Location;
62        System.IO.FileSystemInfo fileInfo = new System.IO.FileInfo( strLoc );
63        string sExeName = fileInfo.Name;
64        bool bCreatedNew;
65
66        mutex = new Mutex( true, "Global\\" + sExeName, out bCreatedNew );
67        if ( bCreatedNew )
68            mutex.ReleaseMutex();
69
70        return !bCreatedNew;
71    }
72
73    static Mutex mutex;
74    const int SW_RESTORE = 9;
75}
Usage
Put this code in your application, and whenever you would call Application.Run, call SingleInstanceApp.Run.