stop multiple instance of same program
-
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
-
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
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
-
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
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[^]
-
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
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 );
#endregionpublic 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
-
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
Just use a named mutex as I outline in my post on single instance applications in Windows CE