在我的应用程序中,我使用定时器来检查RSS订阅中的更新,如果发现新项目,我会弹出一个自定义对话框来通知用户。当我手动运行检查时,一切正常,但是当自动检查在计时器Elapsed事件中运行时,自定义对话框没有显示。
首先,这是一个线程问题吗?(我假设它是因为手动和自动检查都使用相同的代码)。
当我运行自动检查时,我是否需要从Timers Elapsed事件处理程序调用运行检查的方法?
我的自定义对话框类中是否有什么需要做的事情?
编辑:这是一个WinForms应用程序。
以下是代码示例(请不要指出此代码示例中的语法错误,这只是一个简单的示例,而不是真实的代码)。
阅读了一些评论和答案后,我认为我的问题在于我使用的是
编辑:
更改为 Forms.Timer 后测试结果良好(但尚未存在新项目,因此尚未看到自定义对话框)。我添加了一些代码,在调用更新方法时将线程 ID 输出到文件中。使用 Timers.Timer 时,线程 ID 不是 GUI 线程,但使用 Forms.Timer 时,线程 ID 与 GUI 相同。
首先,这是一个线程问题吗?(我假设它是因为手动和自动检查都使用相同的代码)。
当我运行自动检查时,我是否需要从Timers Elapsed事件处理程序调用运行检查的方法?
我的自定义对话框类中是否有什么需要做的事情?
编辑:这是一个WinForms应用程序。
以下是代码示例(请不要指出此代码示例中的语法错误,这只是一个简单的示例,而不是真实的代码)。
public class MainForm : System.Windows.Forms.Form
{
//This is the object that does most of the work.
ObjectThatDoesWork MyObjectThatDoesWork = new ObjectThatDoesWork();
MyObjectThatDoesWork.NewItemsFound += new NewItemsFoundEventHandler(Found_New_Items);
private void Found_New_Items(object sender, System.EventArgs e)
{
//Display custom dialog to alert user.
}
//Method that doesn't really exist in my class,
// but shows that the main form can call Update for a manual check.
private void Button_Click(object sender, System.EventArgs e)
{
MyObjectThatDoesWork.Update();
}
//The rest of MainForm with boring main form stuff
}
public class ObjectThatDoesWork
{
System.Timers.Timer timer;
public ObjectThatDoesWork()
{
timer = new System.Timers.Timer();
timer.Interval = 600000;
timer.AutoReset = true;
timer.Elapsed += new new System.Timers.ElapsedEventHandler(TimeToWork);
timer.Start();
}
private void TimeToWork(object sender, System.Timers.ElapsedEventArgs e)
{
Update();
}
public void Update()
{
//Check for updates and raise an event if new items are found.
//The event is consumed by the main form.
OnNewItemsFound(this);
}
public delgate void NewItemsFoundEventHandler(object sender, System.EventArgs e);
public event NewItemsFoundEventHandler NewItemsFound;
protected void OnNewItemsFound(object sender)
{
if(NewItemsFound != null)
{
NewItemsFound(sender, new System.EventArgs());
}
}
}
阅读了一些评论和答案后,我认为我的问题在于我使用的是
System.Timers.Timer
而不是 System.Windows.Forms.Timer
。编辑:
更改为 Forms.Timer 后测试结果良好(但尚未存在新项目,因此尚未看到自定义对话框)。我添加了一些代码,在调用更新方法时将线程 ID 输出到文件中。使用 Timers.Timer 时,线程 ID 不是 GUI 线程,但使用 Forms.Timer 时,线程 ID 与 GUI 相同。