This question already has answers here:
Thread.Sleep for less than 1 millisecond
(6 answers)
Closed 8 years ago.
I need to delay some piece of code from processing for some time in Microseconds.
I tried a lots of things such as thread.Sleep(x), but it has only milliseconds resolution.
I am using .NET 2.0 in MS Visual Studio 2013.
System.Diagnostics.Stopwatch
allows you to get very small (few nanosecond) ticks, which you can convert to microseconds using this. Then you can just let an empty while loop for as long as you need.
Edit: and it even is supported by .NET 2.0.
Related
This question already has answers here:
How to Query an NTP Server using C#?
(6 answers)
Closed 3 years ago.
I need to get the same time on all Instances of my program. How do I do that. DateTime.Now is not accurate(is different on different hardware) enough, I need to get the Time down to 100 ms difference-precision.
You don't want it read locally from each computer, as you don't know that each PC's clock is perfectly in sync (and you can see that they aren't).
So you have two options:
Write something on each computer to maintain precise time.
Get a web based time and all of your computers will be reading the same data. Here is an example from SO: How to get DateTime from the internet?
This question already has answers here:
High resolution timer in C#
(5 answers)
Closed 5 years ago.
No matter where I look, I can't find a good answer to this question. I'd like to have something happen at a given BPM (in my example, I'm using BPM), but the basic C# Timer class isn't working for me. Since it only measures in milliseconds, any actions performed within the timer get noticeably unsynced from the music. I've attempted to use this MicroTimer Library but with no luck! Though it can be quite fine grained, it's resource heavy and it doesn't have the resolution necessary. I understand I can have a function with a counter, but is there a good way to do this with Visual Studio's libraries (like the basic timer)? I hear those aren't as processor hungry.
I doubt you'll get the kind of time resolution you're looking for in a managed language like C#.
Hell, even if you were writing in C the OS could decide another process is more important and just like that you're out of sync.
Maybe consider using the timer, but resyncing every second or half second? I'd default to another user if they have experience in this area, but I'd at least give that a shot. Or go by the system clock ticks?
This question already has answers here:
Alternative to Thread.Sleep in C#?
(10 answers)
async/await with thread timer
(2 answers)
Keeping UI responsive during a Thread.Sleep() [duplicate]
(3 answers)
Closed 5 years ago.
I'm porting a library to .NET core and to maximize portability I need to eliminate the dependency on System.Threading.Thread, and therefore Thread.Sleep. Whats an alternative to this?
you can use
Task.Delay(2000).Wait(); // Wait 2 seconds with blocking
await Task.Delay(2000); // Wait 2 seconds without blocking
This question already has answers here:
C# time in microseconds
(4 answers)
How can I get the Windows system time with millisecond resolution?
(10 answers)
Closed 5 years ago.
Need to get the Timestamp value from system time with high resolution (microseconds).
I googled and found that we should use StopWatch. But stopwatch is used for difference between of two different time, is it correct?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Execute function at specific time
I want to run certain function at certain time.I tried Timer control.it's not working. My question is:How can I run a function every day at the 19:00 in C#?
Is there any way to check the time and have a Timer object?
Timer code:
int Interval(TimeSpan gorevZamani)
{
if ((gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds > 0.0)
return (int)(gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds;
else
return (int)((gorevZamani - DateTime.Now.TimeOfDay).Add(TimeSpan.FromDays(1)).TotalMilliseconds);
}
in the question set as possible duplicate: C# Execute function at specific time people suggest to use either Quartz.NET or windows Task Scheduler.
Both options could eventually serve the purpose but I believe, as I suggested already few times in similar previous questions, Windows Task Scheduler is better because you no code anything for it and let Windows do the scheduling for you and you focus only on the real business case of your application, which is what Windows cannot do for you, then rely on existing technologies to glue things together and don't have to debug or reinvent what has been done and is available for you anyway.
Use a scheduled task. A good way to do this is the at command, documented on MSDN here.