Best way to implement singleton in a console application C#?

后端 未结 4 1047
夕颜
夕颜 2020-12-16 18:23

I have a console application that is server based. I only want 1 instance of it running at once for a particular server (this is regardless of the user who might be running

4条回答
  •  甜味超标
    2020-12-16 19:07

    Took a lot of assembling bits and pieces of code from everywhere, but I finally found the magic sauce that conceptually creates a singleton console app that can also continue to receive command line arguments. So the first time this is ran, it processes its command line params and then waits. When you try to run this again, if the first is still running, those command line arguments are passed to the first process for handling, and the second process dies.

    using System;
    using System.IO;
    using System.IO.Pipes;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace SingletonConsoleApp
    {
        class Program 
        {
            const string InterprocessID = "{D2D6725E-79C3-4988-8475-4446549B6E6D}"; // can be anything that's unique
            static Mutex appSingletonMaker = new Mutex(true, InterprocessID);
    
            static void Main(string[] args)
            {
                if (appSingletonMaker.WaitOne(TimeSpan.Zero, true))
                {
                    var argHandler = new Action((arguments =>
                    {
                        Console.WriteLine(String.Join(" ", arguments));
                    }));
                    Task.Run(() =>
                    {
                        using (var server = new NamedPipeServerStream(InterprocessID))
                        {
                            using (var reader = new StreamReader(server))
                            {
                                using (var writer = new StreamWriter(server))
                                {
                                    while (true)
                                    {
                                        server.WaitForConnection();
                                        var incomingArgs = reader.ReadLine().Split('\t');
                                        writer.WriteLine("done");
                                        writer.Flush();
                                        server.Disconnect();
                                        argHandler(incomingArgs);
                                    }
                                }
                            }
                        }
                    });
                    argHandler(args);
                    Console.ReadKey();
                    appSingletonMaker.ReleaseMutex();
                }
                else
                {
                    if (args.Length > 0)
                    {
                        using (var client = new NamedPipeClientStream(InterprocessID))
                        {
                            client.Connect();
                            var writer = new StreamWriter(client);
                            using (var reader = new StreamReader(client))
                            {
                                writer.WriteLine(String.Join("\t", args));
                                writer.Flush();
                                reader.ReadLine();
                            }
                        }
                    }
                }
            }
        }
    }
    

提交回复
热议问题