private void Begin() { Configuration.UserAgent = typeof(App).Assembly.GetName().Name + "/" + typeof(App).Assembly.GetName().Version.ToString() + " (https://github.com/Paszt/weather-frog)"; Configuration.ApiKey = My.Settings.WeatherApiKey; notifyIcon = new() { Icon = Utilities.CreateIcon(16, 16, (ImageSource)FindResource("FrogHeadDrawingImage")), DataContext = Current, ContextMenu = (System.Windows.Controls.ContextMenu)FindResource("NotifyIconMenu"), TrayPopup = new TaskbarBalloon(), //ToolTip = new Resources.TaskbarBalloon(), }; // Add bindings to notifyicon MultiBinding toolTipMultiBinding = new() { StringFormat = "Weather Frog \n{0}° {1}\n{2}\nLast updated: {3}", Bindings = { new Binding("Forecast.CurrentWeather.Temp") { Source = this }, new Binding("Forecast.CurrentWeather.Condition.Text") { Source = this }, new Binding("Forecast.Location.DisplayName") { Source = this }, new Binding(nameof(LastUpdatedTimeString)) { Source = this }, } }; BindingOperations.SetBinding(notifyIcon, TaskbarIcon.ToolTipTextProperty, toolTipMultiBinding); Locations = My.Settings.Locations; SystemEvents sysEvents = new(); sysEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged; sysEvents.ResumedFromSuspension += SystemEvents_ResumedFromSuspension; sysEvents.AboutToSuspend += SystemEvents_AboutToSuspend; if (My.Settings.UpdateDesktop) { desktopWallpaper = DesktopWallpaper.FromSystemParameters(); } updateWeatherTimer = new(e => UpdateWeather(), null, TimeSpan.Zero, UpdateWeatherInterval); }
/// <summary> /// Restarts a process that has disappeared. /// </summary> /// <param name="processExePath">Path to the process executable file.</param> /// <param name="processArguments">Arguments to use when restarting the process, empty if no argument.</param> /// <param name="delay">A delay between the time the process has disappeared and the time it's restarted.</param> /// <param name="restartMessage">The message to display when a process was restarted.</param> /// <param name="flags">Process restart flags.</param> /// <returns>True if restarted; False if an error occurred.</returns> private static bool RestartProcess(string processExePath, string processArguments, TimeSpan delay, string restartMessage, Flags flags) { if (delay.TotalSeconds > 0) { Thread.Sleep(delay); } ProcessStartInfo StartInfo = new ProcessStartInfo { FileName = processExePath, Arguments = processArguments, WorkingDirectory = Path.GetDirectoryName(processExePath) !, }; if (flags.HasFlag(Flags.NoWindow)) { StartInfo.UseShellExecute = false; StartInfo.CreateNoWindow = true; // Setting this variable will tell the process it's been restarted. The value doesn't matter. StartInfo.EnvironmentVariables[SharedDefinitions.RestartEnvironmentVariable] = "*"; } bool Result; try { Result = Process.Start(StartInfo) != null; Result &= restartMessage.Length > 0; if (Result) { TaskbarBalloon.Show(restartMessage, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)); } } catch { Result = false; } return(Result); } }
/// <summary> /// Entry points of the program. /// </summary> /// <param name="args">Arguments from the ZombifyMe library.</param> /// <returns>0 if the monitored process exited normally, 1 if it was restarted, or a negative value in case of error.</returns> public static int Main(string[] args) { Thread.Sleep(TimeSpan.FromSeconds(5)); // Check arguments; They should be valid since only ZombifyMe is starting us. if (args == null || args.Length < 8) { return(-1); } // Read the ID of the process to monitor. if (!int.TryParse(args[0], out int ProcessId)) { return(-2); } string ProcessExePath = args[1]; string ProcessArguments = args[2]; string ClientName = args[3]; try { // Open the cancel event. This event uses two unique names, one for the ZombifyMe, the other from the client. using EventWaitHandle? CancelEvent = EventWaitHandle.OpenExisting(SharedDefinitions.GetCancelEventName(ClientName)); // Read the delay, in ticks. if (!long.TryParse(args[4], out long DelayTicks)) { return(-4); } TimeSpan Delay = TimeSpan.FromTicks(DelayTicks); if (DelayTicks < 0) { return(-4); } // Read messages. They can be empty. string WatchingMessage = args[5]; string RestartMessage = args[6]; // Read the flags, as a set of bits. if (!int.TryParse(args[7], out int FlagsValue)) { return(-5); } if (FlagsValue == -1) { return(-5); } Flags Flags = (Flags)FlagsValue; // Display the begin message if requested. if (WatchingMessage.Length > 0) { TaskbarBalloon.Show(WatchingMessage, TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(1)); } MonitorProcess(ProcessId, ProcessExePath, ProcessArguments, CancelEvent, Delay, RestartMessage, Flags, out bool IsRestarted); return(IsRestarted ? 1 : 0); } catch { return(-3); } }
private void ShowBalloon(string text) { TaskbarBalloon.Show(text, TimeSpan.FromSeconds(15), OnClicked, text); }