private async void Initialise() { if (!initialised) { initialised = true; logger.Debug($"Starting initilisation task for context {ApplicationName}."); var process = Process.GetProcessesByName(ProcessName).FirstOrDefault(); if (process == null) { logger.Debug($"Process with name {ProcessName} not detected"); process = StartProcess(); } else { logger.Debug($"Process with name {ProcessName} and PID {process.Id} detected."); } if (process == null) { throw new ArgumentException($"Could not find process with name \"{ProcessName}\"."); } this.process = process; int retries = 0; while (!HookProcess() && retries < 20) { await Task.Delay(500); retries += 1; } if (mainWindowHandle != IntPtr.Zero) { logger.Debug($"Hook on {ProcessName} took {retries} retries."); logger.Information($"Initialised {ApplicationName} context."); } else { logger.Error($"Could not hook on to {ApplicationName} after {retries} retries."); ContextExit?.Invoke(this, new EventArgs()); Dispose(); } } else { logger.Information("Already initialised."); } }
private void Exit() { logger.Information($"Shutting down InTray context for {ApplicationName}."); ContextExit?.Invoke(this, new EventArgs()); Dispose(true); }