【发布时间】:2010-09-07 02:49:29
【问题描述】:
我想在每个配置的时间间隔发送某种心跳。我想用调度器定时器来发送它。
Main()
{
create dispatcher timer;
}
void dispacthertimertick()
{
// send heartbeat
}
如何让主线程保持活跃?
问候 拉朱
【问题讨论】:
-
为什么是 WPF 标签?这是 WPF 应用程序还是...
我想在每个配置的时间间隔发送某种心跳。我想用调度器定时器来发送它。
Main()
{
create dispatcher timer;
}
void dispacthertimertick()
{
// send heartbeat
}
如何让主线程保持活跃?
问候 拉朱
【问题讨论】:
放置它的最佳位置是在您的 App.xaml.cs 中。 WPF 中的应用程序负责设置消息循环,因此您不必真正担心它。如果您的 App.xaml 具有 ApplicationDefinition 的构建操作属性(这是默认值),它将发出此启动代码(您可以使用 Reflector 查看):
[STAThread, DebuggerNonUserCode]
public static void Main()
{
App app = new App();
app.InitializeComponent();
app.Run();
}
你需要使用 OnStartup:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
// setup your timer here
}
Console.Read() 确实是一个 hack 并且没有必要,因为可能没有控制台,就像在 Windows 表单中一样。
【讨论】: