Alternative to Timer

2019-09-02 19:45发布

问题:

I'm developing a c# winform application which is based on third party web API's for trading purpose. It deals with real time data coming from internet like currency rates but I'm using 8 timer in my winform to retrieve different data, application gets non-responding because of timers,

what can i do to make this application work fast and smooth?

回答1:

You might want to look into the System.Threading.Timer instead: Timer Class

System.Threading.Timer, which executes a single callback method on a thread pool thread at regular intervals. The callback method is defined when the timer is instantiated and cannot be changed. Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime.

This link has a nice table of comparison between the different timer classes at the bottom:

Comparing the Timer Classes in the .NET Framework Class Library



回答2:

Use threading. Timers (at least the ones you seem to be using) are executed in the GUI's thread. Use BackgroundWorkers (or other threading methods) instead to decouple background tasks from the GUI.