0

There is a power button in WPF, which should start the loading screen (the screen.STARTScreen() function), after which the text blocks will be filled with data, and after 5 seconds the screen.WRITE() function should be called, which will update the data in the text blocks. The problem is that when I press the button, the 2nd function immediately triggers after 5 seconds. I understand that everything works, but not as intended. Changes to the text blocks occur immediately, and only the Red_Click event itself stops.

WPF .NET 4.5 The functions are attached.

public void STARTScreen (ArrayMenu menu, List<TextBox> screenList, ref string LEVEL)

    {

        screenList[0].Text = "0";

        screenList[1].Text = "1";

        screenList[2].Text = "2";

        screenList[3].Text = "3";
        
    }

private void Red_Click(object sender, RoutedEventArgs e)

    {

        screen.STARTScreen(menu, screenList, ref LEVEL);

        screen.WAIT(5000);

        screen.WRITE(menu, screenList, ref LEVEL);

    }

public void WAIT(int milisec)

    {

        Task.Run(async delegate { await Task.Delay(milisec); }).Wait();

    }

public void WRITE(ArrayMenu menu, List<TextBox> screenList, ref string LEVEL)

    {

        screenList[0].Text = menu.getValue(LEVEL);

        screenList[1].Text = menu.getValue(menu.NextKeyOfAddress(ref LEVEL));

        screenList[2].Text = menu.getValue(menu.NextKeyOfAddress(ref LEVEL));

        screenList[3].Text = menu.getValue(menu.NextKeyOfAddress(ref LEVEL));

    }
IAmCoder
  • 3,179
  • 2
  • 27
  • 49
upsnakeup
  • 11
  • 3
  • 1
    *"after 5 seconds the screen.WRITE() function should be called"* ... *"the 2nd function immediately triggers after 5 seconds"*... so, exactly what you want? – Nick is tired Jan 31 '22 at 16:58
  • My form stops for 5 seconds, and then both functions are triggered instantly. I need to make sure that the text in the textblock (from the START Screen function) remains for 5 seconds, after which it is updated with the screen.WRITE function @Nick – upsnakeup Jan 31 '22 at 17:02
  • 2
    Ah, [How to put delay before doing an operation in WPF](https://stackoverflow.com/a/15600004/3270037) should cover it in that case. You're blocking the UI thread, so the first function _does_ update it, but before it's had a chance to update the screen it starts waiting, it doesn't get a chance to update the screen until after your second function has already been called. – Nick is tired Jan 31 '22 at 17:06
  • It worked, thanks. There are problems with the flow right now, but I'll figure it out. – upsnakeup Jan 31 '22 at 17:22
  • 2
    Glad you managed to get there from that, appreciate it wasn't a huge amount to go off (y) – Nick is tired Jan 31 '22 at 17:23

0 Answers0