The differences and usage of various c classes

  • 2020-05-19 05:40:22
  • OfStack

System.Threading.Timer is a simple lightweight timer that USES a callback method and is serviced by thread pool threads. In cases where the user interface must be updated, it is not recommended to use this timer because its callback does not occur on the user interface thread. In such cases, System.Windows.Threading.DispatcherTimer is a better choice because its events are raised on the user interface thread.
Multithread timer
1: System. Threading. Timer
2: System. Timers. Timer

Single-threaded timers for special purposes:
1: System. Windows. Forms. Timer (Windows Forms Timer)
2: System. Windows. Threading. DispatcherTimer (WPF timer);

Multithreaded timer is more powerful, accurate, and extensible;
Single-threaded timers are safer and more convenient for simple tasks like updating Windows Forms controls or WPF.


System.Threading.Timer Is the simplest multithreaded timer. In the following example, the timer is 5 Start timing in seconds 1 Second call Tick Methods. 
publicstaticvoidMain()
{
//5 Start running after seconds and then every other second 1 Second call Tick methods 
Timertmr=newTimer(Tick,"tick...",5000,1000);
Console.ReadLine();
tmr.Dispose();
}
staticvoidTick(objectdata)
{
Console.WriteLine(data);
}

net framework provides another timer System.Timers.Timer.System.Threading.Timer is simply wrapped. The following features have been added.

Component is implemented, so it can be displayed in the designer. One Interval property in place of the Change method replaces one Elapsed event delegated by callback to start and stop the Enabled property of timer, which defaults to false. To avoid confusion caused by Enabled, the Start and Stop methods are provided. Whether the Elapsed time is raised at the end of each specified interval, or whether the AutoReset property is run only after the interval's first end. The SynchronizingObject object for a secure method invocation in WPF or Windows Forms. publicstaticvoidMainThread ()


{
Timertmr=newTimer();
tmr.Interval=500;
tmr.Elapsed+=newElapsedEventHandler(tmr_Elapsed);
tmr.Start();
Console.ReadLine();
tmr.Stop();
Console.ReadLine();
tmr.Start();
Console.ReadLine();
tmr.Dispose();
}
staticvoidtmr_Elapsed(objectsender,ElapsedEventArgse)
{
Console.WriteLine("Tick...");
}

Single-thread timer:
1: System. Windows. Forms. Timer (Windows Forms Timer)
2: System. Windows. Threading. DispatcherTimer (WPF timer);

Single-threaded timers are timers that are designed to belong to their execution environment. If you use Timer of Windows Forms in an Windows service application, the timer event will not be triggered, but will only be triggered in the corresponding environment.

Like System. Timers. Timer1 sample, they also provide the same members (Interval Tick, Start, Stop), but their internal working principle is different,
The timers of WPF and Windows Forms use a message loop mechanism instead of a thread pool generating messages.
This means that the Tick event is always executed on the same thread that created timer, and it also means that if the last Tick message has not been processed, only one Tick message exists in the message loop, even if the interval is longer.

Here are their advantages:
You can forget about thread safety. An Tick event will not be triggered until the first Tick event has been processed. You can update the control directly in the Tick event handling code without calling Control.Invoke or Dispatcher.Invoke.
Take a look at the effect of using a single-threaded timer in Winform:


// Based on the Windows Single-threaded timer for message loops 
privateSystem.Windows.Forms.Timertimer=newTimer(){};
publicForm1()
{
InitializeComponent();
timer.Tick+=newEventHandler(timer_Tick);
timer.Enabled=true;
}
voidtimer_Tick(objectsender,EventArgse)
{
// Simulation of do 1 Some time-consuming operations 
System.Threading.Thread.Sleep(2000);
}

If you run the above code, you'll notice that the UI interface is slow to respond,
The principle is explained above: the single-threaded timer is based on the Windows message loop, and the application processes the timer message synchronously.
The solution to this problem is to use a multithreaded timer: just modify the code to use a multithreaded timer:

// Using multithreaded timers 
privateSystem.Timers.Timertimer=newSystem.Timers.Timer();
publicForm1()
{
InitializeComponent();
timer.Elapsed+=newSystem.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled=true;
}
voidtimer_Elapsed(objectsender,System.Timers.ElapsedEventArgse)
{
// Simulation of do 1 Some time-consuming operations 
System.Threading.Thread.Sleep(2000);
}

The above example shows us the disadvantages of single-threaded timers:
Unless the Tick event handling code executes very quickly, the UI interface becomes very responsive.
So both WPF and Windows Forms timers are great for small tasks, especially those that update the interface. Examples are clock and count displays. Otherwise, you need a multithreaded timer.
Let's say it's 1000, and let's say it's 1 variable plus 1 at a time, plus 12 times and then do what you want to do, and that's it.

Above are 2 kinds of their own use 1, feel that good can be.


Related articles: