// This BG process is the first one and will be the only one. // It has to maintain connection to the UWP app, watch lifecycle and child processes, // and try to re-establish the connection with the APP if it's closed and opened again. static void StartAsMaster() { // Kickstart static constructors. // NOTE: All classes are static instead of instance, because they're tightly coupled singletons. // But static constructors are called on demand (at first access or call to the class), not at startup. IPC.Init(); // Open named pipe that is used exclusively by this background broker process and potentional future // broker instances. It is used to signal wheter there's already existing broker to ensure there's always // only one broker at a time. It's necessary because new instance of UWP app cannot reconnect to preexisting // background process that was spawned by previous instance of the app. var slavePipe = new NamedPipe(UWP.name, 100); // If new connection occurs on the pipe, it means another broker process has been spawned by a new UWP instance // that has no way of knowing of existence of (let alone connecting to) this broker process. // But we can connect to the UWP from here. slavePipe.Connection += () => UWP.Connect(); // Lifecycle & selfclose watchdog. WatchReferences(); // Open exclusive mutex to signify that this is the master mutex process for the app. mutex = new Mutex(false, UWP.name); // TODO: is this even needed? Application.EnableVisualStyles(); // TODO: remove? Application.SetCompatibleTextRenderingDefault(false); // TODO: remove? // Run blockingly. And release the mutex when the app quits. Application.Run(); Close(); }
static void Main() { IPC.Init(); Application.Run(); }