【问题标题】:dispatcher timer for sending periodic data发送周期性数据的调度器定时器
【发布时间】:2010-09-07 02:49:29
【问题描述】:

我想在每个配置的时间间隔发送某种心跳。我想用调度器定时器来发送它。

Main()
{
  create dispatcher timer;

}

void dispacthertimertick()
{
 // send heartbeat
}

如何让主线程保持活跃?

问候 拉朱

【问题讨论】:

  • 为什么是 WPF 标签?这是 WPF 应用程序还是...

标签: c# .net wpf


【解决方案1】:

放置它的最佳位置是在您的 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 表单中一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2016-07-12
    • 2017-08-25
    • 1970-01-01
    • 2016-02-15
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多