Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

myTimer.Interval = 10000;

I want to start it and fire off the Tick event right away:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
383 views
Welcome To Ask or Share your Answers For Others

1 Answer

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()
{
    ...
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...