Home > database >  Pause song for 10 seconds then start playing
Pause song for 10 seconds then start playing

Time:02-01

Working on a Valentines gift for my wife and I wanted the mp3 song to start to play after 10 seconds. What I have works but for some reason it starts right away and I can't figure out why.

    for (int x = 1; x < 20; x  )//60
    {
        switch (x)
        {
            case 19://59
                mplayerlevel2.URL = "All of you John Legend.mp3";
                break;

            default:
                break;

        }
    }

CodePudding user response:

You can set a Task to execute in the future and then keep executing your program in the meantime.

Task
    .Delay(TimeSpan.FromSeconds(10))
    .GetAwaiter()
    .OnCompleted(() =>
    {
        mplayerlevel2.URL = "All of you John Legend.mp3";
    });

CodePudding user response:

Assuming WinForms and a Button Click event, put async in the method signature and use await Task.Delay():

private async void button1_Click(object sender, EventArgs e)
{
    button1.Enabled = false;
    await Task.Delay(10000);
    mplayerlevel2.URL = "All of you John Legend.mp3";
    button1.Enabled = true;
}

CodePudding user response:

i'd use a native sleep function, which is made for this :

System.Threading.Thread.Sleep(10000);
  •  Tags:  
  • c#
  • Related