What is the most efficient method to measure time?

Viewed 1175

I'm in a situation in which I need to do an action X ammount of times per second (refresh a PictureBox or pull bytes from socket buffer). I was wondering if there is a proper way to do this. I did some research and I found few ways of going about this.

  1. I can work in a while loop and constantly ask OS what time is it to check if X ammount of time had passed.(DateTime.UtcNow)

  2. I can work in a while loop and put it to sleep for a period of time.

  3. I can work in a while loop and use Clock class to check for elapsed time.

  4. Use Timer Class to raise event.

These are all ways I know to make something happen periodically. Second method is not very precise but I put it here for completion sake. Are there other methods I should be aware of? Which one has the least overhead?

I'm working in VisualStudio 2015 C# and version of .NET 4.6.1

2 Answers
Related