WPF does not update text field at run time

I have this code:

void wait(int ms) { System.Threading.Thread.Sleep(ms); } private void button1_Click(object sender, RoutedEventArgs e) { info.Text = "step 1"; wait(1000); info.Text = "step 2"; wait(1000); info.Text = "step 3"; wait(1000); info.Text = "step 4"; wait(1000); } 

And the problem is that textbox.text is updated after the void button1_Click chain is complete. It is not updated in AIR :(

Please how to do this?

+4
source share
4 answers

Just do it.

 private void button1_Click(object sender, RoutedEventArgs e) { ThreadPool.QueueUserWorkItem((o) => { Dispatcher.Invoke((Action) (() => info.Text = "step 1")); wait(1000); Dispatcher.Invoke((Action) (() => info.Text = "step 2")); wait(1000); Dispatcher.Invoke((Action) (() => info.Text = "step 3")); wait(1000); Dispatcher.Invoke((Action) (() => info.Text = "step 4")); wait(1000); }); } 
+9
source

The actual stream is not updated until the button1_Click method button1_Click . That is why you only see the last meaning. You should use long methods for asynchronous calls or use threads.

+3
source

Measure / arrangement (and therefore rendering) occurs asynchronously, so if you want to force the screen to refresh, you will need to call UpdateLayout.

0
source

What to do if you tried DispatcherFrame to simulate DoEvents forms:

You may need to enable System.Security.Permissions and System.Windows.Threading. After that, replace the DoEvents () call after each of your sleep and get the desired result.

 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)] public void DoEvents() { DispatcherFrame frame = new DispatcherFrame(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrame), frame); Dispatcher.PushFrame(frame); } public object ExitFrame(object f) { ((DispatcherFrame)f).Continue = false; return null; } 

MSDN article link: http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcher.pushframe.aspx

-1
source

All Articles