Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Mobile
  4. stop multiple instance of same program

stop multiple instance of same program

Scheduled Pinned Locked Moved Mobile
csharp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cory Kimble
    wrote on last edited by
    #1

    Does any one have any code sample that checks for a current instance of the current application running. I have code for .net framwork 2.0 but not for .net compactframework for windows ce

    J G P J 4 Replies Last reply
    0
    • C Cory Kimble

      Does any one have any code sample that checks for a current instance of the current application running. I have code for .net framwork 2.0 but not for .net compactframework for windows ce

      J Offline
      J Offline
      Joel Ivory Johnson
      wrote on last edited by
      #2

      Have your program create a native system event with CreateEvent. The error code returned by GetLastError() after creating an event will be zero if no other program owns an event with that name. When a second instance of your program is created it will get a non-zero error code indicating another program has already created an event with that name. Just remember to call CloseHandle on the event object when your program is no longer executing; you don't want to cause a memory leak. I published something on this site two days ago that will contain all the PInvoke declarations that you need. Check it out at http://www.codeproject.com/KB/mobile/WiMoNativeSync.aspx

      Joel Ivory Johnson (J2i.net) Live Messenger

      1 Reply Last reply
      0
      • C Cory Kimble

        Does any one have any code sample that checks for a current instance of the current application running. I have code for .net framwork 2.0 but not for .net compactframework for windows ce

        G Offline
        G Offline
        Giannakakis Kostas
        wrote on last edited by
        #3

        Another not so perfect solution is to use FindWindow to find out if the window of your application already exists. There is also an implementation of a named mutex in OpenNetCF that you could use. Have a look at http://community.opennetcf.com/forums/p/10438/10438.aspx[^]

        1 Reply Last reply
        0
        • C Cory Kimble

          Does any one have any code sample that checks for a current instance of the current application running. I have code for .net framwork 2.0 but not for .net compactframework for windows ce

          P Offline
          P Offline
          PavanPareta
          wrote on last edited by
          #4

          Hi Cory Kimble, By default CF .NET apps are supposed to be single instance. However sometimes, if you try to launch an app quickly you might get multiple instances. you can use the following code to enforce single instance (http://www.nesser.org/blog/archives/56) Code Snippet

          using System;
          using System.Windows.Forms;
          using System.Runtime.InteropServices;

          namespace SomeNameSpace
          {
          static class SingleInstance
          {
          [MTAThread]
          static void Main()
          {
          // Check if we have a duplicate instance of the program running
          if ( IsInstanceRunning() )
          {
          // Perhaps log the fact a duplicate program was shut down
          return;
          }

          /*
          * Continue with your normal program here
          */

          try
          {
          Application.Run( YourFormClass );
          }
          catch ( Exception e )
          {
          // Log an exception that caused the application to close here
          MessageBox.Show( "See log file for details.\r\nClosing this application.", "Fatal Application Error" );
          }
          }

          #region OpenNETCF native interface to mutex generation (version 1.4 of the SDF)

          public const Int32 NATIVE_ERROR_ALREADY_EXISTS = 183;

          #region P/Invoke Commands for Mutexes
          [DllImport( "coredll.dll", EntryPoint="CreateMutex", SetLastError=true )]
          public static extern IntPtr CreateMutex(
          IntPtr lpMutexAttributes,
          bool InitialOwner,
          string MutexName );

          [DllImport( "coredll.dll", EntryPoint="ReleaseMutex", SetLastError=true )]
          public static extern bool ReleaseMutex( IntPtr hMutex );
          #endregion

          public static bool IsInstanceRunning()
          {
          IntPtr hMutex = CreateMutex( IntPtr.Zero, true, "ApplicationName" );
          if ( hMutex == IntPtr.Zero )
          throw new ApplicationException( "Failure creating mutex: "
          + Marshal.GetLastWin32Error().ToString( "X" ) );

          if ( Marshal.GetLastWin32Error() == NATIVE_ERROR_ALREADY_EXISTS )
          return true;
          else
          return false;
          }
          #endregion
          }
          }

          Pavan Pareta

          1 Reply Last reply
          0
          • C Cory Kimble

            Does any one have any code sample that checks for a current instance of the current application running. I have code for .net framwork 2.0 but not for .net compactframework for windows ce

            J Offline
            J Offline
            Jakob Olsen
            wrote on last edited by
            #5

            Just use a named mutex as I outline in my post on single instance applications in Windows CE

            my blog about C# & .NET programming

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups