Visual Studio delay between multiple startup projects?

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

how to add some delay between startup projects in solution?

\"enter

I want Client

相关标签:
9条回答
  • 2021-02-07 00:04

    Another simpler option for testing is to just delay the client if the debugger is attached like this:

    if (System.Diagnostics.Debugger.IsAttached)
    {
        System.Threading.Thread.Sleep(2000);
    }
    

    You might wrap that in an #if DEBUG block if you like. Anyway I think this should be the least amount of work :)

    0 讨论(0)
  • 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();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-07 00:10

    I would probably add a retry mechanism within the client. That way not only does it help in the "starting up from Visual Studio" case - it also helps if the server happens to be restarting while the real client connects. The fact that the server is on a faster machine doesn't mean the server will never need to restart, does it?

    Indeed, you may well want to add this retry mechanism in such a way that the client can recover even if the server is restarted while it's connected. It depends on what the project is doing, of course.

    0 讨论(0)
提交回复
热议问题