-2

this is my script.

        private async void secondmult_Click(object sender, EventArgs e)
        {
            var sct = sc;
            var secondmultx = secondmult;

            if (player.pcc == 100)
            {
                player.multiplier = 2;
                player.pcc = player.pcc - 100;
                secondmult.Enabled = false;
                sct.Text = player.pcc.ToString();
            }
            else if(player.pcc < 100)
            {
                sc.Text = "X";
                await Thread.Sleep(3000);
                sc.Text = player.pcc.ToString();
            }
            else if(player.pcc > 100)
            {
                player.multiplier = 2;
                player.pcc = player.pcc - 100;
                secondmultx.Enabled = false;
                sct.Text = player.pcc.ToString();
            }

There is an error on the line 'await Thread.Sleep(3000);' It says 'Cannot await 'void'' Please may someone re-do the script for me so it works?

Axqua
  • 35
  • 5

2 Answers2

3

Thread.Sleep() is not awaitable. Use Task.Delay() instead.

CoolBots
  • 4,770
  • 2
  • 16
  • 30
1

Thread.sleep(3000) is not awaitable. Your method is async so ideal implementation will be await Task.Delay(3000)

Thread.Sleep is going to block your current thread and Task.Delay is going to delay logically without blocking your current thread.

Thread.sleep should not be used in asynchronous operation instead we should use Task.Delay(3000) and vice versa.

Dharman
  • 30,962
  • 25
  • 85
  • 135
sobby01
  • 1,916
  • 1
  • 13
  • 22