how to prevent user launch a .net C# appl twice?

  • Thread starter Thread starter Sean
  • Start date Start date
S

Sean

Hello,

need some suggestions of preventing launching multiple instance of the same
appl which is written in C# in .NET CF. I want to do something like the
following:

public void Main() {
// if (an instance of myself is running) {
// exit();
// }

}

Thanks in advance for any suggestions.
Sean
 
On PocketPC that's default behavior. If it's vanilla CE, you need to check
and create a Mutex to determine if you're running.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
It's supposed to be the default behavior. However, it is possible to start
two instances up if you keep quickly tapping on the executable to start the
program. What I do is register a socket listener. If the registration
fails, I exit the program. I'm not sure why I did it that way but it works.
In case you are wondering, yes some real users keep tapping on the executable
until it starts.
 
I always thought the behavior sucked (leave things like this to the
developer to decide if they want it)- here's a case that proves it. :)

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
One of possible ways is to create named event at application startup. If
named event already created then your application is started:

using System.Runtime.InteropServices;
....

static void Main()
{
IntPtr evnt = CreateEvent(IntPtr.Zero, true, true, "MyApplication's
Named Event");
if (Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS) return;

Application.Run(new Form1());

CloseHandle(evnt);
}

[DllImport("coredll", SetLastError=true)]
static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool
manualReset, bool initialState, string name);

[DllImport("coredll")]
static extern bool CloseHandle(IntPtr handle);

const int ERROR_ALREADY_EXISTS = 183;



Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
static void Main()
{
theSocket=new
System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork,
System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
try
{
theSocket.Bind(new
System.Net.IPEndPoint(System.Net.IPAddress.Loopback,4801));
theSocket.Listen(0);
}
catch
{
return;
}
//add whatever is in your main function here.
}
 
I'm betting you did it instead of the Mutex is becasue the CF doesn't
support named mutexes (no clue why). The SDF does have the MutexEx

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
Back
Top