本文介绍了如何以编程方式触发计时器滴答?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个Windows窗体计时器以10秒(10,000毫秒)间隔配置的:

Let's say I have a Windows Forms timer configured with a 10 second (10k ms) interval:

myTimer.Interval = 10000;

我要开始它并关火勾选事件马上

myTimer.Start();
myTimer_Tick(null, null);

最后一行工作,但有没有更好或更合适的方式?

The last line works but is there a better or more appropriate way?

推荐答案

我会做不同的唯一一件事就是实际滚动功能移到一个单独的方法,让你不必直接调用事件做。

The only thing I'd do differently is to move the actual Tick functionality into a separate method, so that you don't have to call the event directly.

myTimer.Start();
ProcessTick();

private void MyTimer_Tick(...)
{
    ProcessTick();
}

private void ProcessTick()
{
    ...
}

首先,我会做这个作为事件的直接调用对我来说似乎是一个 - 通常表示spagetti结构code

Primarily, I'd do this as direct calling of events seems to me to be a Code Smell - often it indicates spagetti structure code.

这篇关于如何以编程方式触发计时器滴答?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 18:12