Note that System.Timers.Timer is only one of several possible timers you can use in WinForms. I suggest you read this CodeProject article, and download/study/run its code samples, to review the different timers: "All about .NET Timers - A Comparison" [
^].
This example uses a System.Timers.Timer to call a function regularly:
private const int UpdateServerInterval = 5;
private Int64 msToUpdate = new TimeSpan(0,UpdateServerInterval,0).Milliseconds;
private System.Timers.Timer UpdateServerTimer = new System.Timers.Timer;
private delegate void updateServer();
private updateServer doUpdateServer;
private void YourMainForm_Load(object sender, EventArgs e)
{
UpdateServerTimer = new System.Timers.Timer(msToUpdate);
UpdateServerTimer.AutoReset = true;
UpdateServerTimer.Elapsed += UpdateServerTimerOnElapsed;
doUpdateServer+= DoUpdateServer;
}
private void btnStartTheTimer_Click(object sender, EventArgs e)
{
UpdateServerTimer.Start();
}
private void UpdateServerTimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
this.Invoke(doUpdateServer);
}
private void DoUpdateServer()
{
}
Note that in this example a separate Thread is not used to update: that means the WinForms UI is going to be blocked during the execution of the code in 'DoUpdateServer. My guess is that you will want to use a separate Thread for the update, depending on the design of your application, and how it is to be used at run-time.