How does a windows form timer work internally?

1

What happens is that I'm using two Timer in a form , the first one checks a folder, if you have added new files and put the names in a queue (FIFO) and the other is responsible for being Verifying if the queue has elements uncoils them and makes other processes.

The problem is that my application is becoming very slow. The problem is that I do not know how a timer works internally? If they are threads that are executed in each determined time that is indicated to him. If it's not like that, I think it's convenient for me to change them by threads.

    
asked by Alberto Rojas 23.11.2016 в 00:02
source

1 answer

4

That depends on which timer you are using, in .net framework there are at least four timer classes.

Timers that use threads:

  

System.Timers.Timer
  System.Threading.Timer
  System.Web.UI.Timer (ASP.NET)

Timer that does not use threads:

  

System.Windows.Forms.Timer

The latter works using the Windows message cycle, that is, the timer event will be treated in the same thread in which the other window events are treated (keyboard input, screen drawing etc), and not will allow to treat user events or drawing on screen while a timer event is being processed.

With a timer of this type your vantana message queue looks something like this:

  

WM_KEYBOARD - > the user typed something in WM_PAINT - > There's something that   paint on screen
WM_MOUSEMOVE - > the mouse moved
  WM_TIMER - > there is a pending timer event

Another disadvantage of this timer is that, having to wait for other events in the message queue to be treated, it is much less accurate than its counterparts with threads.

The main advantage is that the implementation is simpler. For threads with threads you have to think about synchronization, deadlocks, race-conditions (no idea how to say that in Spanish), etc.

    
answered by 23.11.2016 / 00:29
source