简体   繁体   中英

Windows Forms async await

While I using winforms app, each 5 minute I need to check data updates. I need to send request to few service then get response and update data in database. What's is the best practices to make on another thread (or task?)? The program should not slow down. I try to make with timer: Init timer when program is running

public class Timer
{

    private System.Timers.Timer timer;
    private void InitTimer()
    {
        timer = new System.Timers.Timer(4000);
        timer.Elapsed += ElapsedTime;
        timer.Enabled = true;
    }

    private void ElapsedTime()
    {
      //send request and update data
    }
}

The way you are doing it will work just fine. Thedocumentation for Sytem.Timers.Timer says:

If the SynchronizingObject property is null , the Elapsed event is raised on a ThreadPool thread.

The SynchronizingObject property is null by default, so your Elasped event will run on a ThreadPool thread, not on the UI thread. That means it will not stop your application from responding to user input.

If there is a chance that ElapsedTime() will run longer than your interval, and you don't want the events overlapping, then you can set AutoReset to false and reset it manually at the end of ElapsedTime() . Just make sure that everything is wrapped in a try / catch block, otherwise the timer won't get reset if there's an exception. My code below shows how that would look.

You don't need to use async / await anywhere here. Since it won't be running on the UI thread, using asynchronous code won't really help you any. In a desktop app, it's not a big deal to have a separate (non-UI) thread wait.

public class Timer
{

    private System.Timers.Timer timer;
    private void InitTimer()
    {
        timer = new System.Timers.Timer(4000);
        timer.Elapsed += ElapsedTime;
        timer.AutoReset = false;
        timer.Enabled = true;
    }

    private void ElapsedTime()
    {
        try {
            //send request and update data
        }
        catch (Exception e)
        {
            //log the error
        }
        finally
        {
            //start the timer again
            timer.Enabled = true;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM