-1

I have created a c# service which I can run as standalone (console) application from console and also as a windows service. I want to stop run these two (stand alone exe and windows service) to run simultaneously. I want to stop launching this application as a service if i have already started this application from console as a standalone application (or) I want to stop this application starting from console if i have already stated this application as a service. i have tried some of the approaches available (Mutex approach) but they are working only for "stop launching same application twice (2 standalone applications)" but not for "stand alone windows application and windows service".

static void Main()
{
    bool createdNew = true;
    using (Mutex mutex = new Mutex(true, "MyService.exe", out createdNew))
    {
        if (createdNew)
        {
            //new app
            MyService service = new MyService();

            //Launch as console application
            if (Environment.UserInteractive)
            {
                service.OnStartForConsole();

                if (service.IsServiceStartedSuccessfully)
                {
                     //wait for user to quite the application 
                      Console.ReadLine();
                    }
                }
                service.OnStopForConsole();
            }
            else
            {
                //Launching as windows service 
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                   service
                };
                ServiceBase.Run(ServicesToRun);
            }
        }
        else
        {
            Process current = Process.GetCurrentProcess();
            foreach (Process process in Process.GetProcessesByName(current.ProcessName))
            {
                if (process.Id != current.Id)
                {
                    Console.WriteLine("Already started the proces.");
                    break;
                }
            }
        }
    }
}

can anyone help me how can I achieve this.

thanks in advance.

NDestiny
  • 1,133
  • 1
  • 12
  • 28
  • A mutex does not care about the type of process that created it. Try the canonical https://stackoverflow.com/questions/229565/what-is-a-good-pattern-for-using-a-global-mutex-in-c (which sets global permissions) & if that does not work you need to edit your question with some code and a description of what goes wrong. – Alex K. Aug 01 '18 at 13:24
  • 1
    I think some of this ways can help you to solve your problem https://stackoverflow.com/questions/6392031/how-to-check-if-another-instance-of-the-application-is-running – Che Aug 01 '18 at 13:31
  • @Che it helped, thanks. – NDestiny Aug 01 '18 at 13:53
  • Don't append answers to questions. Instead, post an answer. If you do make sure to not pick the most brittle solution imaginable. This one fails in case the executable is copied using a different name. And killing a process instead of just ending it looks wrong, no matter how you look at it. – IInspectable Aug 01 '18 at 14:09

1 Answers1

0
//This has worked finally, but at the cost of @IInspectable comments.
if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1) System.Diagnostics.Process.GetCurrentProcess().Kill();
NDestiny
  • 1,133
  • 1
  • 12
  • 28