Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio delay between multiple startup projects?

how to add some delay between startup projects in solution?

enter image description here

I want Client project to be started after 2-3 seconds from starting WindowsService.

Why I need this?

WindowsService runs socket server and Client runs socket to connect to server. WindowsService loads slowly than Client, and this causes an exception on client side when connecting to server which is not run yet

like image 671
Lev Avatar asked Apr 27 '12 10:04

Lev


People also ask

How do I change the startup project in Visual Studio 2019?

Choose the name of the project which you want to set default startup project. Click to the Project on toolbar and then click Set as startup project.


2 Answers

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.

like image 129
Jon Skeet Avatar answered Sep 23 '22 16:09

Jon Skeet


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();         }     } } 
like image 31
Romil Kumar Jain Avatar answered Sep 19 '22 16:09

Romil Kumar Jain