Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GUI not updating until end of event

Tags:

c#

wpf

I have a gui that needs to be updated from a hardware device attached through a dll file and a textbox. My problem is that gui is not updated until the end of the event and I need to show something pause and then show something else. The hack of Application.DoWork didn't change anything. Anyone have any suggestions? Everything I was reading used either invoke or DoEvents and neither seem to change the behavior.

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {

            App.myMainWindow.image1.Visibility = Visibility.Hidden;
            System.Windows.Forms.Application.DoEvents();


        }
        Thread.Sleep(4000);
    }
like image 799
haywire Avatar asked Jan 31 '26 01:01

haywire


1 Answers

You make the GUI thread sleep, obviously the GUI cannot be updated when its thread sleeps. Create a seperate thread and use the Dispatcher to update UI-elements if you must, you can savely send that thread to sleep and your GUI will still respond.

Edit: System.Windows.Forms.Application.DoEvents(); you are sure about that WPF tag, aren't you?

like image 123
H.B. Avatar answered Feb 02 '26 18:02

H.B.