public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Thread HeartRateThread = new Thread(startThread);
HeartRateThread.Name = "Class1";
HeartRateThread.Start();
}
private void startThread(object obj)
{
new Class1();
}
}
public class Class1
{
public Class1()
{
DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0,0,0,1);
timer1.Tick += timer1_tick;
timer1.Start();
}
private void timer1_tick(object sender, EventArgs e)
{
Debug.WriteLine("timer called");
}
}
我正在尝试从另一个线程启用此 timer_tick 函数,因为这在 maInWindow 的代码部分很明显。但是,调用 Class1 构造函数但未启用 timertick 功能。但是,如果我在主线程上执行此操作,则一切正常。这有什么原因。我怎样才能让它工作?