public static InfoWindow Create(string title, string text, string image_url, string action_name, Action action) { lock (lock_object) { InfoWindow w = null; Action a = () => { w = new InfoWindow(title, text, image_url, action_name, action); WindowInteropHelper h = new WindowInteropHelper(w); h.EnsureHandle(); w.Show(); ThreadRoutines.StartTry(() => { Thread.Sleep(Settings.Default.InfoToastLifeTimeInSecs * 1000); w.BeginInvoke(() => { w.Close(); }); }); if (!string.IsNullOrWhiteSpace(Settings.Default.InfoSoundFile)) { SoundPlayer sp = new SoundPlayer(Settings.Default.InfoSoundFile); sp.Play(); } }; lock (ws) { if (dispatcher == null) {//!!!the following code does not work in static constructor because creates a deadlock!!! ThreadRoutines.StartTry(() => { //this window is used to hide notification windows from Alt+Tab panel invisible_owner_w = new Window(); invisible_owner_w.Width = 0; invisible_owner_w.Height = 0; invisible_owner_w.WindowStyle = WindowStyle.ToolWindow; invisible_owner_w.ShowInTaskbar = false; invisible_owner_w.Show(); invisible_owner_w.Hide(); dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher; System.Windows.Threading.Dispatcher.Run(); }, null, null, true, ApartmentState.STA); if (!SleepRoutines.WaitForCondition(() => { return(dispatcher != null); }, 3000)) { throw new Exception("Could not get dispatcher."); } } } dispatcher.Invoke(a); return(w); } }
public static bool TryKill(this Process process, int timeoutMss = 1000, int pollTimeSpanMss = 300) { return(SleepRoutines.WaitForCondition(() => { try { process.Kill(); } catch { // Process already exited. return true; } process.WaitForExit(pollTimeSpanMss); return !process.IsRunning(); }, timeoutMss )); }
public static object WaitForCondition(this WebBrowser browser, Func <object> check_condition, int timeout_in_mss) { DateTime timeout = DateTime.Now.AddMilliseconds(timeout_in_mss); while (DateTime.Now < timeout) { object o = null; browser.Invoke(() => { if (browser.Document != null && browser.Document.Body != null) { o = check_condition(); } }); if (o != null) { return(o); } SleepRoutines.Wait(20); } return(null); }
void save(object sender, EventArgs e) { try { ushort v; //if (!ushort.TryParse(ServerDefaultPort.Text, out v)) // throw new Exception("Server port must be an integer between 0 and " + ushort.MaxValue); //general.TcpClientDefaultPort = v; if (string.IsNullOrWhiteSpace(ServerDefaultIp.Text)) { throw new Exception("Default server ip is not specified."); } IPAddress ia; if (!IPAddress.TryParse(ServerDefaultIp.Text, out ia)) { throw new Exception("Default server ip is not a valid value."); } general.TcpClientDefaultIp = ia.ToString(); if (!ushort.TryParse(ClientPort.Text, out v)) { throw new Exception("Client port must be an between 0 and " + ushort.MaxValue); } general.TcpServerPort = v; if (string.IsNullOrWhiteSpace(ServiceDomain.Text)) { throw new Exception("Service domian is not specified."); } general.ServiceDomain = ServiceDomain.Text.Trim(); if (string.IsNullOrWhiteSpace(ServiceType.Text)) { throw new Exception("Service type is not specified."); } general.ServiceType = ServiceType.Text.Trim(); if (Monitors.SelectedIndex < 0) { throw new Exception("Captured Video Source is not specified."); } general.CapturedMonitorDeviceName = (string)Monitors.SelectedValue; general.ShowMpegWindow = ShowMpegWindow.IsChecked ?? false; general.WriteMpegOutput2Log = WriteMpegOutput2Log.IsChecked ?? false; //general.CapturedMonitorRectangle = MonitorRoutines.GetMonitorAreaByMonitorName(general.CapturedMonitorDeviceName); //if (general.CapturedMonitorRectangle == null) // throw new Exception("Could not get rectangle for monitor '" + general.CapturedMonitorDeviceName + "'"); general.CapturedMonitorRectangle = null; general.DeleteLogsOlderDays = int.Parse(DeleteLogsOlderDays.Text); UiApiClient.SaveServiceSettings(general); if (Settings.View.DeleteLogsOlderDays != general.DeleteLogsOlderDays) { Settings.View.DeleteLogsOlderDays = general.DeleteLogsOlderDays; Settings.View.Save(); } System.ServiceProcess.ServiceControllerStatus?status = UiApiClient.GetServiceStatus(); if (status != null && status != System.ServiceProcess.ServiceControllerStatus.Stopped && (ProcessRoutines.ProcessIsSystem() ||/*used for configuration during installing*/ Message.YesNo("The changes have been saved and will be engaged after service restart. Would you like to restart the service (all the present connections if any, will be terminated)?") ) ) { MessageForm mf = null; ThreadRoutines.StartTry(() => { mf = new MessageForm(System.Windows.Forms.Application.ProductName, System.Drawing.SystemIcons.Information, "Resetting the service. Please wait...", null, 0, null); mf.ShowDialog(); }); UiApiClient.StartStopService(false); UiApiClient.StartStopService(true); if (null == SleepRoutines.WaitForObject(() => { return(mf); }, 1000)) { throw new Exception("Could not get MessageForm"); } mf.Invoke(() => { mf.Close(); }); } Close(); } catch (Exception ex) { Message.Exclaim(ex.Message); } }
public static AlertWindow Create(string title, string text, string image_url, string action_name, Action action) { lock (lock_object) { Action a = () => { if (This != null) { try {//might be closed already This.Close(); } catch { } } if (invisible_owner_w == null) { //this window is used to hide notification windows from Alt+Tab panel invisible_owner_w = new Window(); invisible_owner_w.WindowStyle = WindowStyle.ToolWindow; invisible_owner_w.Width = 0; invisible_owner_w.Height = 0; //invisible_owner_w.Width = SystemParameters.FullPrimaryScreenWidth; //invisible_owner_w.Height = SystemParameters.FullPrimaryScreenHeight; //invisible_owner_w.AllowsTransparency = true; //invisible_owner_w.Background = Brushes.Transparent; //invisible_owner_w.Topmost = true; invisible_owner_w.ShowInTaskbar = false; invisible_owner_w.Top = 0; invisible_owner_w.Left = 0; //invisible_owner_w.MouseDown += delegate //{ // invisible_owner_w.Hide(); //}; //invisible_owner_w.KeyDown += delegate //{ // invisible_owner_w.Hide(); //}; invisible_owner_w.Show(); invisible_owner_w.Hide(); } This = new AlertWindow(title, text, image_url, action_name, action); //ElementHost.EnableModelessKeyboardInterop(This); WindowInteropHelper wih = new WindowInteropHelper(This); This.handle = wih.EnsureHandle(); This.Owner = invisible_owner_w; This.Show(); //ThreadRoutines.StartTry(() => //{ // Thread.Sleep(Settings.Default.InfoWindowLifeTimeInSecs * 1000); // This.BeginInvoke(() => { This.Close(); }); //}); if (!string.IsNullOrWhiteSpace(Settings.Default.InfoSoundFile)) { SoundPlayer sp = new SoundPlayer(Settings.Default.AlertSoundFile); sp.Play(); } }; if (dispatcher == null) {//!!!the following code does not work in static constructor because creates a deadlock!!! ThreadRoutines.StartTry(() => { dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher; System.Windows.Threading.Dispatcher.Run(); }, null, null, true, ApartmentState.STA); if (!SleepRoutines.WaitForCondition(() => { return(dispatcher != null); }, 3000)) { throw new Exception("Could not get dispatcher."); } } dispatcher.Invoke(a); //ControlRoutines.InvokeFromUiThread(a); return(This); } }
public static InfoWindow Create(string title, string text, string image_url, string action_name, Action action, string sound_file = null, Brush box_brush = null, Brush button_brush = null) { InfoWindow w = null; if (text.Length > Settings.View.InfoToastMaxTextLength) { text = text.Remove(Settings.View.InfoToastMaxTextLength, text.Length - Settings.View.InfoToastMaxTextLength) + "<...>"; } Action a = () => { w = new InfoWindow(title, text, image_url, action_name, action); w.SetAppearance(box_brush, button_brush); WindowInteropHelper h = new WindowInteropHelper(w); h.EnsureHandle(); w.Show(); ThreadRoutines.StartTry(() => { Thread.Sleep(Settings.View.InfoToastLifeTimeInSecs * 1000); w.Dispatcher.BeginInvoke((Action)(() => { w.Close(); })); }); if (string.IsNullOrWhiteSpace(sound_file)) { sound_file = Settings.View.InfoSoundFile; } sound_file = PathRoutines.GetAbsolutePath(sound_file); SoundPlayer sp = new SoundPlayer(sound_file); sp.Play(); }; lock (ws) { if (dispatcher == null) {//!!!the following code does not work in static constructor because creates a deadlock!!! dispatcher_t = ThreadRoutines.StartTry(() => { if (invisible_owner_w == null) {//this window is used to hide notification windows from Alt+Tab panel invisible_owner_w = new Window(); invisible_owner_w.Width = 0; invisible_owner_w.Height = 0; invisible_owner_w.WindowStyle = WindowStyle.ToolWindow; invisible_owner_w.ShowInTaskbar = false; invisible_owner_w.Show(); invisible_owner_w.Hide(); } if (dispatcher == null) { //dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher; dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread); System.Windows.Threading.Dispatcher.Run(); } }, null, null, true, ApartmentState.STA); if (!SleepRoutines.WaitForCondition(() => { return(dispatcher != null); }, 3000)) { throw new Exception("Could not get dispatcher."); } } } dispatcher.Invoke(a); return(w); }