(C# WPF) I'm using 2 awaits in a loop and they're going all over the place












0















Here's the code I'm trying to make work:



public async Task PlayAudioAsync(int id)
{

stop = false;
while (!stop)
{
music.Position = TimeSpan.FromSeconds(0);
music2.Position = TimeSpan.FromSeconds(0);
music.Play();
if (id < 1)
{

await Task.Delay(16000);
music2.Play();

await Task.Delay(16000);
}
else
{

await Task.Delay(51200);
music2.Play();

await Task.Delay(51200);
}
}
stop = false;
}


The first question that you'd ask first is probably that why am I using two music players? I want the end of the sound loops play as well, there's reverb effects and the notes fade smoothly.



The second loop should start after 16 seconds if id is 0.
The first loop should restart after 16 seconds is id is 0.
In this case, the id is 0.



What happens?



First time they play as they should.
After that they either don't play at all or play at the same time.

After that they do the opposite of what they just did.
And so on...



Also this is my first post, so I almost know what I'm doing here...










share|improve this question

























  • While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

    – Nkosi
    Nov 24 '18 at 21:56











  • What is the purpose of the delay?

    – Nkosi
    Nov 24 '18 at 22:27











  • The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

    – Pate Jate
    Nov 24 '18 at 22:31











  • You lost me at starting the loop at a specific moment. You need to clarify that last comment

    – Nkosi
    Nov 24 '18 at 23:00











  • Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

    – Pate Jate
    Nov 24 '18 at 23:10
















0















Here's the code I'm trying to make work:



public async Task PlayAudioAsync(int id)
{

stop = false;
while (!stop)
{
music.Position = TimeSpan.FromSeconds(0);
music2.Position = TimeSpan.FromSeconds(0);
music.Play();
if (id < 1)
{

await Task.Delay(16000);
music2.Play();

await Task.Delay(16000);
}
else
{

await Task.Delay(51200);
music2.Play();

await Task.Delay(51200);
}
}
stop = false;
}


The first question that you'd ask first is probably that why am I using two music players? I want the end of the sound loops play as well, there's reverb effects and the notes fade smoothly.



The second loop should start after 16 seconds if id is 0.
The first loop should restart after 16 seconds is id is 0.
In this case, the id is 0.



What happens?



First time they play as they should.
After that they either don't play at all or play at the same time.

After that they do the opposite of what they just did.
And so on...



Also this is my first post, so I almost know what I'm doing here...










share|improve this question

























  • While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

    – Nkosi
    Nov 24 '18 at 21:56











  • What is the purpose of the delay?

    – Nkosi
    Nov 24 '18 at 22:27











  • The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

    – Pate Jate
    Nov 24 '18 at 22:31











  • You lost me at starting the loop at a specific moment. You need to clarify that last comment

    – Nkosi
    Nov 24 '18 at 23:00











  • Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

    – Pate Jate
    Nov 24 '18 at 23:10














0












0








0


1






Here's the code I'm trying to make work:



public async Task PlayAudioAsync(int id)
{

stop = false;
while (!stop)
{
music.Position = TimeSpan.FromSeconds(0);
music2.Position = TimeSpan.FromSeconds(0);
music.Play();
if (id < 1)
{

await Task.Delay(16000);
music2.Play();

await Task.Delay(16000);
}
else
{

await Task.Delay(51200);
music2.Play();

await Task.Delay(51200);
}
}
stop = false;
}


The first question that you'd ask first is probably that why am I using two music players? I want the end of the sound loops play as well, there's reverb effects and the notes fade smoothly.



The second loop should start after 16 seconds if id is 0.
The first loop should restart after 16 seconds is id is 0.
In this case, the id is 0.



What happens?



First time they play as they should.
After that they either don't play at all or play at the same time.

After that they do the opposite of what they just did.
And so on...



Also this is my first post, so I almost know what I'm doing here...










share|improve this question
















Here's the code I'm trying to make work:



public async Task PlayAudioAsync(int id)
{

stop = false;
while (!stop)
{
music.Position = TimeSpan.FromSeconds(0);
music2.Position = TimeSpan.FromSeconds(0);
music.Play();
if (id < 1)
{

await Task.Delay(16000);
music2.Play();

await Task.Delay(16000);
}
else
{

await Task.Delay(51200);
music2.Play();

await Task.Delay(51200);
}
}
stop = false;
}


The first question that you'd ask first is probably that why am I using two music players? I want the end of the sound loops play as well, there's reverb effects and the notes fade smoothly.



The second loop should start after 16 seconds if id is 0.
The first loop should restart after 16 seconds is id is 0.
In this case, the id is 0.



What happens?



First time they play as they should.
After that they either don't play at all or play at the same time.

After that they do the opposite of what they just did.
And so on...



Also this is my first post, so I almost know what I'm doing here...







c# wpf async-await






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 22:24







Pate Jate

















asked Nov 24 '18 at 21:49









Pate JatePate Jate

11




11













  • While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

    – Nkosi
    Nov 24 '18 at 21:56











  • What is the purpose of the delay?

    – Nkosi
    Nov 24 '18 at 22:27











  • The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

    – Pate Jate
    Nov 24 '18 at 22:31











  • You lost me at starting the loop at a specific moment. You need to clarify that last comment

    – Nkosi
    Nov 24 '18 at 23:00











  • Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

    – Pate Jate
    Nov 24 '18 at 23:10



















  • While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

    – Nkosi
    Nov 24 '18 at 21:56











  • What is the purpose of the delay?

    – Nkosi
    Nov 24 '18 at 22:27











  • The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

    – Pate Jate
    Nov 24 '18 at 22:31











  • You lost me at starting the loop at a specific moment. You need to clarify that last comment

    – Nkosi
    Nov 24 '18 at 23:00











  • Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

    – Pate Jate
    Nov 24 '18 at 23:10

















While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

– Nkosi
Nov 24 '18 at 21:56





While this feels like an XY problem. I get the nagging feeling that you may need to research TaskCompletionSource

– Nkosi
Nov 24 '18 at 21:56













What is the purpose of the delay?

– Nkosi
Nov 24 '18 at 22:27





What is the purpose of the delay?

– Nkosi
Nov 24 '18 at 22:27













The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

– Pate Jate
Nov 24 '18 at 22:31





The delay is for starting the loop at a specific moment, I don't want to just wait until the loop has played all the way through.

– Pate Jate
Nov 24 '18 at 22:31













You lost me at starting the loop at a specific moment. You need to clarify that last comment

– Nkosi
Nov 24 '18 at 23:00





You lost me at starting the loop at a specific moment. You need to clarify that last comment

– Nkosi
Nov 24 '18 at 23:00













Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

– Pate Jate
Nov 24 '18 at 23:10





Oh... By loop I meant an audio loop, not a programming loop. Like how you keep hearing the same melody in a song.

– Pate Jate
Nov 24 '18 at 23:10












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%2f53462662%2fc-wpf-im-using-2-awaits-in-a-loop-and-theyre-going-all-over-the-place%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%2f53462662%2fc-wpf-im-using-2-awaits-in-a-loop-and-theyre-going-all-over-the-place%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

Wiesbaden

Marschland

Dieringhausen