Visual Studio delay between multiple startup projects?

前端 未结 9 1321
暖寄归人
暖寄归人 2021-02-06 23:31

how to add some delay between startup projects in solution?

\"enter

I want Client

9条回答
  •  渐次进展
    2021-02-07 00:05

    You can use Mutex locking to sync the two startup project.

    Program 1 (StartUp Project 1):

        namespace ConsoleApplication1
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading;
    
        class Program1
        {
            private static  bool isNewMutexCreated = true;
            private static Mutex mutex;
            static void Main(string[] args)
            {
                mutex = new Mutex(true, "Global\\ConsoleApplication1", out isNewMutexCreated);
                AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
                Console.WriteLine("Application1 executed on " + DateTime.Now.ToString());
    
                Console.ReadKey();
            }
    
            static void CurrentDomain_ProcessExit(Object sender, EventArgs e)
            {
                if (isNewMutexCreated)
                {
                    Console.WriteLine("Mutex Released");
                    mutex.ReleaseMutex();
                }
            }
    
        }
    }
    

    Program 2 (StartUp Project 2):

    namespace ConsoleApplication2
    {
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.IO;
        using System.Threading;
    
        class Program2
        {
            static void Main(string[] args)
            {
                Mutex mutex = null;
                Thread.Sleep(5000);
    
                while (mutex == null)
                {
                    try
                    {
                        mutex = Mutex.OpenExisting("Global\\ConsoleApplication1");
    
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Mutex not found on " + DateTime.Now.ToString());
                        Thread.Sleep(3000);
                    }
    
    
                }
                Console.WriteLine("Application2 executed on " + DateTime.Now.ToString());
                Console.ReadKey();
            }
        }
    }
    

提交回复
热议问题