Check for Previous Instance of App

Last post 03-12-2004 6:53 by Anonymous. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 08-09-2002 12:40

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Check for Previous Instance of App

    Credit: Chris Tacke


    private const int ERROR_ALREADY_EXISTS = 183;

    [DllImport("coredll.dll", EntryPoint="GetLastError")]
    private static extern int GetLastError();

    [DllImport("coredll.dll", EntryPoint="CreateMutexW")]
    private static extern int CreateMutex(IntPtr lpMutexAttributes, bool InitialOwner, string MutexName);

    public static bool IsInstanceRunning()
    {

    string appname = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

    if(CreateMutex(IntPtr.Zero, true, appname) != 0)
    {
    if(GetLastError() == ERROR_ALREADY_EXISTS)
    {
    DEBUGMSG(true, "Already running\r\n");
    return true;
    }
    else
    {
    DEBUGMSG(true, "Not already running\r\n");
    return false;
    }
    }

    DEBUGMSG(true, "CreateMutex failed!\r\n");
    return false;
    }


  • 09-04-2002 1:59 In reply to

    Re: Check for Previous Instance of App

    Do you know how to set focus to instance running ??

  • 09-07-2002 2:43 In reply to

    Re: Check for Previous Instance of App

    quote:

    Do you know how to set focus to instance running ??




    This should get you started (it uses PInvoke and FindWindow/IsIconic/ShowWindow/SetForegroundWindow API calls):

    using System.Threading;
    ...
    [STAThread]
    static void Main()
    {
    //allow only one instance
    bool ownsmutex;

    Mutex m = new Mutex(true, Application.ProductName, out ownsmutex);
    if (!ownsmutex)
    {
    //bring old instance to the front
    IntPtr hwnd = Win32.API.FindWindow(null, "MainFormTitle");
    if (hwnd != IntPtr.Zero)
    {
    if (Win32.API.IsIconic(hwnd))
    Win32.API.ShowWindow(hwnd, Win32.API.ShowState.SW_SHOWNOACTIVATE);
    Win32.API.SetForegroundWindow(hwnd);
    }

    //exit this application
    Environment.Exit(0);
    return;
    }
    Application.Run(new MainForm());
    }


    If you need the PInvoke code (in my Win32.API namespace), email me privately at stormfire@@@bigfoot.com.
    ShaneB

  • 09-07-2002 2:50 In reply to

    Re: Check for Previous Instance of App

    My apologies if this is a compact framework discussion...

    I thought the question was regarding desktop versions of Windows and I posted a bit too hastily.

    ShaneB

  • 02-10-2003 20:42 In reply to

    Re: Check for Previous Instance of App

    Assuming that this thread refers to the Compact Framework, it is no longer neccessary to check for previous app instances. The CF does it for us. If there is already an instance of an application running when the user selects the app from the Start menu, the CF will use the currently running instance instead of spawning a new instance.
    [:)]

    Dave Ranck
  • 02-11-2003 18:03 In reply to

    • ctacke
    • OpenNETCF Staff
    • Top 10 Contributor
    • Joined on 07-27-2007
    • Indiana
    • Posts 1,868

    Re: Check for Previous Instance of App

    Actually that's not 100% true. For PocketPC it is, but CE 4.x still requires that you look for an existing instance yourself.
  • 06-03-2003 12:00 In reply to

    Re: Check for Previous Instance of App

    Here is the VB.Net implementation:
    <code>
    Public Class PrevInstance
    Const ERROR_ALREADY_EXISTS As Integer = 183

    <System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint:="GetLastError")> _
    Private Shared Function GetLastError() As Integer
    End Function

    <System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint:="CreateMutexW")> _
    Private Shared Function CreateMutex(ByVal lpMutexAttributes As IntPtr, ByVal InitialOwner As Boolean, ByVal MutexName As String) As Integer
    End Function

    Public Shared Function IsInstanceRunning() As Boolean
    Try
    Dim appname As String = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
    If CreateMutex(IntPtr.Zero, True, appname) <> 0 Then
    If GetLastError() = ERROR_ALREADY_EXISTS Then
    Return True
    Else
    Return False
    End If
    End If
    Return False
    Catch ex As Exception
    MsgBox("IsInstanceRunning Error: " & ex.ToString)
    End Try
    End Function 'IsInstanceRunning
    End Class
    </code>

    Scott
  • 03-12-2004 6:53 In reply to

    Re: Check for Previous Instance of App

    Here's my suggestion:

    public frmMain()
    {
    try
    {

    SocketListener.Bind(new IPEndPoint(IPAddress.Loopback, 1033));
    InitializeComponent();
    }
    catch
    {
    Application.Exit();
    }
    }


    only one app can hear at one port. is there an app listening at port x, so your app runs at 2nd start into an error.

    ~~~~~~~~~~

    no code no food
Page 1 of 1 (8 items)