首先我不知道这是不是一个愚蠢的问题。我有这种情况,首先我有一个主窗口
public MainWindow()
{
InitializeComponent();
//dt is a System.Windows.Threading.DispatcherTimer variable
dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 30000);
dt.Tick += new EventHandler(refreshData);
dt.Start();
}
refreshData 方法执行此操作:
public void refreshData(object sender, EventArgs e)
{
Conection c = new Conection();
//this method just returns 'hello' doesn't affect my problem
c.sayHello();
}
这个主窗口也有一个按钮,当我点击按钮时,我会调用另一个类
private void button1_Click(object sender, RoutedEventArgs e)
{
ShowData d = new ShowData();
d.Show();
}
这个类与主窗口非常相似,它也有一个自己的 DispatcherTimer
public ShowData()
{
InitializeComponent();
dt = new System.Windows.Threading.DispatcherTimer();
dt.Interval = new TimeSpan(0, 0, 0, 0, 30000);
dt.Tick += new EventHandler(refreshData);
dt.Start();
}
public void refreshData(object sender, EventArgs e)
{
Conection c = new Conection();
c.sayHello();
}
我使用 Visual Studio 调试器跟踪对 sayHello 的调用,问题是当我关闭“ShowData”窗口时,仍然出现从 ShowData 类对 sayHello 的调用
我没有正确关闭窗户吗?关闭窗口后如何停止通话?
PS:我尝试在 on_closing 事件中将 DispatcherTimer 设置为 null