DispatcherTimer 未触发 Tick 事件

发布于 2024-10-26 09:30:22 字数 370 浏览 8 评论 0原文

我有一个像这样初始化的 DispatcherTimer:

static DispatcherTimer _timer = new DispatcherTimer();

static void Main()
{
    _timer.Interval = new TimeSpan(0, 0, 5);
    _timer.Tick += new EventHandler(_timer_Tick);
    _timer.Start();
}
static void _timer_Tick(object sender, EventArgs e)
{
    //do something
}

_timer_Tick 事件永远不会被触发,我错过了什么吗?

I have a DispatcherTimer i have initialised like so:

static DispatcherTimer _timer = new DispatcherTimer();

static void Main()
{
    _timer.Interval = new TimeSpan(0, 0, 5);
    _timer.Tick += new EventHandler(_timer_Tick);
    _timer.Start();
}
static void _timer_Tick(object sender, EventArgs e)
{
    //do something
}

The _timer_Tick event never gets fired, have i missed something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烟花易冷人易散 2024-11-02 09:30:22

如果这是您的主入口点,则很可能(几乎可以肯定)Main 方法会在第一个 DispatcherTimer 事件发生之前退出。

一旦 Main 完成,该进程就会关闭,因为没有其他前台线程。

话虽如此,DispatcherTimer 实际上仅在拥有 Dispatcher 的用例(例如 WPF 或 Silverlight 应用程序)中才有意义。对于控制台模式应用程序,您应该考虑使用 Timer 类,即:

static System.Timers.Timer _timer = new System.Timers.Timer();

static void Main()
{
    _timer.Interval = 5000;
    _timer.Elapsed  += _timer_Tick;
    _timer.Enabled = true;

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(); // Block until you hit a key to prevent shutdown
}
static void _timer_Tick(object sender, ElapsedEventArgs e)
{
    Console.WriteLine("Timer Elapsed!");
}

If this is your main entry point, it's likely (near certain) that the Main method exits prior to when the first DispatcherTimer event could ever occur.

As soon as Main finishes, the process will shut down, as there are no other foreground threads.

That being said, DispatcherTimer really only makes sense in a use case where you have a Dispatcher, such as a WPF or Silverlight application. For a console mode application, you should consider using the Timer class, ie:

static System.Timers.Timer _timer = new System.Timers.Timer();

static void Main()
{
    _timer.Interval = 5000;
    _timer.Elapsed  += _timer_Tick;
    _timer.Enabled = true;

    Console.WriteLine("Press any key to exit...");
    Console.ReadKey(); // Block until you hit a key to prevent shutdown
}
static void _timer_Tick(object sender, ElapsedEventArgs e)
{
    Console.WriteLine("Timer Elapsed!");
}
萌逼全场 2024-11-02 09:30:22

因为主方法线程在调用tick之前就结束了。

because the main method thread ended before the tick was called.

错爱 2024-11-02 09:30:22

你错过了Application.Run()。如果没有调度程序循环,则无法调度 Tick 事件。第二个问题是您的程序在引发事件之前立即终止。 Application.Run() 也解决了这个问题,它阻塞了 Main() 方法。

You missed Application.Run(). Tick events cannot be dispatched without the dispatcher loop. A secondary issue is that your program immediately terminates before the event ever could be raised. Application.Run() solves that too, it blocks the Main() method.

旧伤还要旧人安 2024-11-02 09:30:22

如果计时器是在工作线程中创建的,则 Tick 事件将不会触发,因为没有调度程序。

您必须在 UI 线程中创建计时器或使用 DispatcherTimer 构造函数,该构造函数将 Dispatcher 实例作为第二个参数,并传递 UI 调度程序:

var timer = new DispatcherTimer(DispatcherPriority.Background, uiDispatcher);

If the timer is created in a worker thread, the Tick event will not fire because there is not dispatcher.

You have to create the timer in the UI thread or use the DispatcherTimer constructor which takes a Dispatcher instance as the second argument, passing the UI dispatcher:

var timer = new DispatcherTimer(DispatcherPriority.Background, uiDispatcher);
故事还在继续 2024-11-02 09:30:22

您必须启动调度程序才能让调度程序“执行”任何事件。如果您在 WPF 应用程序内部运行,这应该会自动发生。如果您在控制台中运行(看起来像),则永远不会触发,因为没有调度程序。您可以做的最简单的事情就是在 WPF 应用程序中尝试此操作,它应该可以正常工作。

You have to start a dispatcher in order for the dispatcher to "do" any events. If you are running inside of a WPF application, this should happen automatically. If you are running in a console (which it looks like), this will never fire because there isn't a dispatcher. The easiest thing you can do is try this in a WPF application and it should work fine.

梦幻之岛 2024-11-02 09:30:22

你必须

static void DispatcherTimer_Tick(object sender, object e)
{

}

在timer_tick事件的地方使用

You have to use

static void DispatcherTimer_Tick(object sender, object e)
{

}

in the place of timer_tick event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文