Beispiel #1
0
        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 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);
        }