how to add some delay between startup projects in solution?
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
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.
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.
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(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With