PopupNotifier在计时器类中不起作用

时间:2018-09-12 07:30:03

标签: c# wpf timer

PopupNotifier不起作用,当将其放置在timer class内时,如果我们将PopupNotifier放在定时器类的外面,对我来说工作正常,下面的代码使用为此。

public MainWindow()
        {
            InitializeComponent();
            bool needToShowInTaskbar = this.ShowInTaskbar;
            WindowState initialWindowState = this.WindowState;
             // making window invisible
            this.ShowInTaskbar = false;
            this.WindowState = WindowState.Minimized;
             // showing and hiding window
            this.Show();
            this.Hide();
             // restoring original settings
            this.ShowInTaskbar = needToShowInTaskbar;
            this.WindowState = initialWindowState;
            SetTimer();
     }

     private  void SetTimer()
            {
                m_mainTimer = new System.Timers.Timer();
                m_mainTimer.Interval = 60000;   // every one min
                m_mainTimer.Elapsed += m_mainTimer_Elapsed;
                m_mainTimer.AutoReset = false;  // makes it fire only once
                m_mainTimer.Start(); // Start
                m_timerTaskSuccess = false;
            }

        private void m_mainTimer_Elapsed(Object source, ElapsedEventArgs e)
            {
               try
                {
                    LoadData();
                    PopupNotifier popup = new PopupNotifier();
                    popup.TitleText = joomla.subject;
                    popup.ContentText = joomla.body;
                    popup.Popup();
                    m_timerTaskSuccess = true;
                }
                catch (Exception ex)
                {
                    m_timerTaskSuccess = false;
                }
                finally
                {
                    if (m_timerTaskSuccess)
                    {
                        m_mainTimer.Start();
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

将计时器更改为System.Windows.Threading.DispatcherTimer时,对我来说效果很好。

 private  void SetTimer()
        {
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);

            dispatcherTimer.Interval = new TimeSpan(0, 0, 60);
            dispatcherTimer.Start();

        }