Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I have developed application using winforms [c#], operated by mutex, so that it can be operated only once at a time.
Lately it became necessary to allow this program to have another instance - for other purposes, with no connection to the first instance, of cource.

So, i don't want to remove the mutex since i don't want to allow users open it 100 times [like notepad] and also to prevent mistakes when user doesn't notice that the program already in work..
I also tried to change my project name to another in order to make the program work as a different one, and i also changed the exe file name by changing its assembly default name [in solution properties],
but the change didn't work well and i still had many refference errors.

I'd be thankful for every idea you can think about.
Thanks !
Philippe Mori 13-Sep-11 17:32pm
As suggested by some people, I suggest not to limit the number of instance. Also if the number of instance is limited, then solution that allows a limited number of instance other than one might not works properly in one instance crashes or is killed. Thus it can require log off or reboot that will only frustrate your users.

So change it to allow two instances, or just remove the mutex and ignore the non-issue.
Just remove the single instance limitation and be done with it. Why do we limit applications to single instance? Because it makes sense doesn't cut it. The reasons to limit an application to a single instance are finite, but the requests to do so are ubiquitous. That tells me it's being overused.

Do you really care if the application is started 100 times? Do yourself a favor: remove the constraint and spend your programming time elsewhere.
Sergey Alexandrovich Kryukov 13-Sep-11 15:45pm
I agree, my 5. Nevertheless, I provided one of the possible method of limiting of number of running instances to some predefined number > 1. Please see my solution.
Sergey Alexandrovich Kryukov 13-Sep-11 16:41pm
I really appreciate you decent self-criticism related to this solution, but I still think this advice has perfect sense; OP should better attend to it and think well about your suggestion.
Most likely, this code is designed to keep number of instances 0 to 1; more exactly, second instance of loading runs for a short time until it discovers that the first instance is already running, to terminate immediately. If you remove this code, you will be able load your application any number of times, but you cannot make this number, say, 3 or 4 and no more. Without looking at the code, it's hard to say how to modify it; and most likely it would not make any sense.

What happens with you is the usual consequence of using some other person's code without understanding how it works. And then you try to modify behavior using trial-end-error, blind-folded approach. This is not how such things can be achieved.

Most likely, you will need to write a brand new code to achieve what you want. But first think if you really need it and why. There is a number of ways to limit number of application instances during run time. One simple approach is this: in your application, run System.Diagnostics.Process.GetProcessesByName(string) using the name of current process. Some of such processes can have the same name by coincidence, so examine Process.MainModule for each process and System.Diagnostics.ProcessModule.FileName to make sure it is loaded from the same executable file, in your case, same as main executable module of your entry assembly.

Based on this information, calculate the total number of processes running a separate instance of your application and decide if you want to run another one. If not, terminate your current process immediately.


tgrt 13-Sep-11 15:48pm
Much nicer approach to the OP than my solution. :)
Sergey Alexandrovich Kryukov 13-Sep-11 16:39pm
Thank you very much, not sure it worth the effort, won't hurt anyway, I hope... :-)
Where a Mutex is used to allow single access to a resource, a System.Threading.Semaphore can allow multiple access. The coding is very similar e.g.

internal sealed class Limiter {
  private const String SemaphoreName = "AARDVARK.Samphire";
  private const Int32 HardCodedLimit = 2;
  private Semaphore sem;

  internal Limiter() {
    sem = new Semaphore(HardCodedLimit, HardCodedLimit, SemaphoreName);

  internal Boolean PermissionGranted() {
    return sem.WaitOne(0);

  internal void Release() {

and then use to control application instance count

private static Limiter limiter;

internal static void Main() {
  // Limited instance count
  limiter = new Limiter();
  if (limiter.PermissionGranted()) {
    Application.Run(new Form1());
  } else {

I've never done this myself but that's the general idea.

Member 7928594 3-Oct-11 19:51pm
Well this works just greate, but now I'd like to ask you:
is there any way that the program name will be effected from the instance?
I only need to help the user to distiguish between the two instances somehow...
Do you have any idea ? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900