1
 switch (Pattern[i])
        {
            case "Blue":
                blueButton.BackColor = Color.LightBlue;
                Thread.Sleep(1000);
                blueButton.BackColor = Color.Red;
                break;
            case "Lime":
                limeButton.BackColor = Color.Aquamarine;
                Thread.Sleep(1000);
                limeButton.BackColor = Color.Red;
                break;
            case "Yellow":
                yellowButton.BackColor = ColorTranslator.FromHtml("#FFFF80");
                Task.Delay(1000).Wait();
                yellowButton.BackColor = Color.Red;
                break;
            case "Red":
                redButton.BackColor = Color.Pink;
                Task.Delay(1000);
                redButton.BackColor = Color.Red;
                break;
        }    

But there's simply no delay... it just jumps through. I tried using both Task.Delay and Thread.Sleep but non of them actually delay. Also I'm a new programmer so take it easy on me.

quadroid
  • 8,444
  • 6
  • 49
  • 82
L B
  • 49
  • 1
  • 6
  • 1
    It's necessary to provide the actual problem you're solving, not just your broken solution. – zerkms Nov 11 '17 at 08:19
  • And you are sure the value is red, yellow, lime or blue? – Peter Bons Nov 11 '17 at 08:20
  • I'm just trying to make it look like a button is being pressed. Also it does go into the cases, I checked. it just skips the delay and switched to red. – L B Nov 11 '17 at 08:21
  • See https://stackoverflow.com/questions/952906/ – H H Nov 11 '17 at 08:24
  • The problem is not in changing the color. The idea is it switched to a lighter color and back to normal, so you need the delay to be able to see the change. I changed everything to red to debug. – L B Nov 11 '17 at 08:26
  • it jumps through to Red you mean right? did you try a bigger delay just for test? – oetoni Nov 11 '17 at 08:38
  • 1
    You switch the color but block the UI Thread, therefore no updates are visual on the Window. You could work with await Task.Delay(1000) – quadroid Nov 11 '17 at 08:39
  • If you are testing with the final case, that will not work, Task.Delay without a call to Wait() or without awaiting it in an async method with not delay it will just carry on like nothing happened – Dave Nov 11 '17 at 08:41
  • I did roll back your edit, don't post complete programs, simple examples that are able to reproduce the error are enough. – quadroid Nov 11 '17 at 09:04

1 Answers1

4

Thread.Sleep does delay the execution as expected, but you run this code on the same Thread that is printing the UI therefore no Update is visible.

An easy fix for this is Task.Delay (you need to mark the function as async):

 case "Blue":
     blueButton.BackColor = Color.LightBlue;
     await Task.Delay(1000);
     blueButton.BackColor = Color.Red;
     break;

To mark the function async add the async keyword to the function call

 private async Task showPattern(List<string> Pattern)

You should read some basics about windows GUI Thread and async programming.

A very basic explanation of the UI Thread: When you call Form.Show or Form.ShowDialog the application is starting a so called message loop. This message loop does process mouse and keyboard events of the window shown. This message loop does aswell handle the drawing of the window. Therefore if this thread is locked up, you application becomes unresponsive. A Button Click handler (and all other handlers of you Window) are processed by this same Thread. If you block the execution in one of this handlers (as you did with Thread.Delay) the window is unresponsive (not processing events and not redrawing) in this Time.

To overcome this issue one solution is to work with async and await . This two methods do a lot of compiler magic to make this work as one would expect (check the link for more information, you definitely should!)

quadroid
  • 8,444
  • 6
  • 49
  • 82