Reputation: 35
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);
}
Upvotes: 3
Views: 645
Reputation: 35
I figured it out using timers as vittore had suggested. This must be the way to do it since you cannot sleep the GUI thread during GUI event handlers.
public partial class MainWindow : Window
{
static Timer _timer;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
this.image1.Visibility = Visibility.Hidden;
this.image2.Visibility = Visibility.Visible;
_timer = new Timer(2000);
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
_timer.Enabled = true; // Enable it
}
}
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
textBox1.Dispatcher.Invoke(new Action(delegate()
{
_timer.Enabled = false;
this.image1.Visibility = Visibility.Visible;
this.image2.Visibility = Visibility.Hidden;
}));
}
Upvotes: 0
Reputation: 50672
I'd use a backgroundworker and start it in the click handler.
This way the GUI will continue to be available and progress can be displayed.
Upvotes: 0
Reputation: 184441
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?
Upvotes: 2
Reputation: 17579
Take a look at this web page for beginning http://www.c-sharpcorner.com/UploadFile/jieying/UsingProgressBarStatusBarandTimerControlsinVS.NET11282005021220AM/UsingProgressBarStatusBarandTimerControlsinVS.NET.aspx
Upvotes: 1