Weird behaviour of Thread.Sleep & Task.Delay












0















I started to develop a simple console metronome and I noticed that something gone wrong with the timing between beats.



After I set the tempo and start the metronome the beeps is working properly for a while and then it break the monotone rhythm and start to play the beep sound inaccurately with a strange rhythm.



Furthermore the duration of beep sound start to be shorter (cut it), along the tempo increasing.





I always got the error after a few second at 200 ms, so just test it at this tempo, but when you decrease it (mean increase the break between beeps), the issue will come later and later until it's totally disappear (about at 7-800 ms).



By the way, it doesn't matter what method I use (Sleep or Delay)...





Here a very simplified code which contain the problem.



using System;
using System.Threading;
using System.Threading.Tasks;

namespace Temporary
{
class Program
{
static void Main()
{
while (true)
{
Console.Beep(2000, 200);
Thread.Sleep(200);
//Task.Delay(200).Wait();
}
}
}
}


Any idea what is this error and how can I fix it? Thanks!










share|improve this question























  • Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

    – styx
    Nov 25 '18 at 12:06











  • Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

    – Hans Passant
    Nov 25 '18 at 12:17











  • @styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

    – taffer
    Nov 25 '18 at 13:07













  • @taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

    – BushWookie
    Nov 25 '18 at 14:26











  • @styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

    – BushWookie
    Nov 25 '18 at 14:29
















0















I started to develop a simple console metronome and I noticed that something gone wrong with the timing between beats.



After I set the tempo and start the metronome the beeps is working properly for a while and then it break the monotone rhythm and start to play the beep sound inaccurately with a strange rhythm.



Furthermore the duration of beep sound start to be shorter (cut it), along the tempo increasing.





I always got the error after a few second at 200 ms, so just test it at this tempo, but when you decrease it (mean increase the break between beeps), the issue will come later and later until it's totally disappear (about at 7-800 ms).



By the way, it doesn't matter what method I use (Sleep or Delay)...





Here a very simplified code which contain the problem.



using System;
using System.Threading;
using System.Threading.Tasks;

namespace Temporary
{
class Program
{
static void Main()
{
while (true)
{
Console.Beep(2000, 200);
Thread.Sleep(200);
//Task.Delay(200).Wait();
}
}
}
}


Any idea what is this error and how can I fix it? Thanks!










share|improve this question























  • Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

    – styx
    Nov 25 '18 at 12:06











  • Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

    – Hans Passant
    Nov 25 '18 at 12:17











  • @styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

    – taffer
    Nov 25 '18 at 13:07













  • @taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

    – BushWookie
    Nov 25 '18 at 14:26











  • @styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

    – BushWookie
    Nov 25 '18 at 14:29














0












0








0


1






I started to develop a simple console metronome and I noticed that something gone wrong with the timing between beats.



After I set the tempo and start the metronome the beeps is working properly for a while and then it break the monotone rhythm and start to play the beep sound inaccurately with a strange rhythm.



Furthermore the duration of beep sound start to be shorter (cut it), along the tempo increasing.





I always got the error after a few second at 200 ms, so just test it at this tempo, but when you decrease it (mean increase the break between beeps), the issue will come later and later until it's totally disappear (about at 7-800 ms).



By the way, it doesn't matter what method I use (Sleep or Delay)...





Here a very simplified code which contain the problem.



using System;
using System.Threading;
using System.Threading.Tasks;

namespace Temporary
{
class Program
{
static void Main()
{
while (true)
{
Console.Beep(2000, 200);
Thread.Sleep(200);
//Task.Delay(200).Wait();
}
}
}
}


Any idea what is this error and how can I fix it? Thanks!










share|improve this question














I started to develop a simple console metronome and I noticed that something gone wrong with the timing between beats.



After I set the tempo and start the metronome the beeps is working properly for a while and then it break the monotone rhythm and start to play the beep sound inaccurately with a strange rhythm.



Furthermore the duration of beep sound start to be shorter (cut it), along the tempo increasing.





I always got the error after a few second at 200 ms, so just test it at this tempo, but when you decrease it (mean increase the break between beeps), the issue will come later and later until it's totally disappear (about at 7-800 ms).



By the way, it doesn't matter what method I use (Sleep or Delay)...





Here a very simplified code which contain the problem.



using System;
using System.Threading;
using System.Threading.Tasks;

namespace Temporary
{
class Program
{
static void Main()
{
while (true)
{
Console.Beep(2000, 200);
Thread.Sleep(200);
//Task.Delay(200).Wait();
}
}
}
}


Any idea what is this error and how can I fix it? Thanks!







c# runtime-error beep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 11:53









BushWookieBushWookie

92




92













  • Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

    – styx
    Nov 25 '18 at 12:06











  • Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

    – Hans Passant
    Nov 25 '18 at 12:17











  • @styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

    – taffer
    Nov 25 '18 at 13:07













  • @taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

    – BushWookie
    Nov 25 '18 at 14:26











  • @styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

    – BushWookie
    Nov 25 '18 at 14:29



















  • Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

    – styx
    Nov 25 '18 at 12:06











  • Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

    – Hans Passant
    Nov 25 '18 at 12:17











  • @styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

    – taffer
    Nov 25 '18 at 13:07













  • @taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

    – BushWookie
    Nov 25 '18 at 14:26











  • @styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

    – BushWookie
    Nov 25 '18 at 14:29

















Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

– styx
Nov 25 '18 at 12:06





Thread.sleep() or Task.Delay() are not accurate if you to get metronome like sound you should use something more accurate like (Multimedia Timers)[docs.microsoft.com/en-us/windows/desktop/Multimedia/…

– styx
Nov 25 '18 at 12:06













Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

– Hans Passant
Nov 25 '18 at 12:17





Doubtful anybody would get a easy repro. Thread.Sleep() and Task.Delay() use the exact same underlying OS resource. There is no service guarantee, the OS considers everything else that ask for service as well. The kind that plays havoc on most programmer's machines when they make an executable file appear from nowhere is anti-malware. Temporarily disable it to see if that makes a difference. If not and you see no obvious activity in Task Manager then changing the Thread.Priority is not inappropriate.

– Hans Passant
Nov 25 '18 at 12:17













@styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

– taffer
Nov 25 '18 at 13:07







@styx: Though Thread.Sleep can be inaccurate for very tight delays (shorter than 15ms) it is actually quite precise for >50 ms ranges. Here must be some other problem. @BushWookie: For myself I could not reproduce it on my 8 core computer even with decreasing the delays to 50ms. Do you execute it without the debugger attached?

– taffer
Nov 25 '18 at 13:07















@taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

– BushWookie
Nov 25 '18 at 14:26





@taffer: Yes, it perhaps something else than accuracy issue, but I have no idea what... I tried to run without debugging and standalone as well, but the problem still there.

– BushWookie
Nov 25 '18 at 14:26













@styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

– BushWookie
Nov 25 '18 at 14:29





@styx: I'm a beginner yet and the Multimedia Timers looks like quite complex on my level. I try to learn it maybe it will fix the problem but it's difficult, but thanks!

– BushWookie
Nov 25 '18 at 14:29












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467146%2fweird-behaviour-of-thread-sleep-task-delay%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467146%2fweird-behaviour-of-thread-sleep-task-delay%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno