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();
}
}
}
答案 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();
}