Reputation: 65
In my c# app I am trying to change a button's background image and change it back after a few seconds. However, the background image is not changed until after the timer is up and then is instantly changed back to its original image before you can see the change.
private void button1_Click(object sender, EventArgs e)
{
myImage.BackgroundImage = Properties.Resources.newImage;
System.Threading.Thread.Sleep(5000);
myImage.BackgroundImage = Properties.Resources.myImage;
}
Upvotes: 0
Views: 56
Reputation: 7918
Assuming this is a WPF app, add the DispatcherTimer
as shown in the following code snippet:
DispatcherTimer _dispatcherTimer = new DispatcherTimer();
public MainWindow()
{
InitializeComponent();
button1.Click += button1_Click;
_dispatcherTimer.Tick += new EventHandler(dt_Tick);
_dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 5);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
myImage.BackgroundImage = Properties.Resources.newImage;
_dispatcherTimer.Start();
}
void dt_Tick(object sender, EventArgs e)
{
_dispatcherTimer.Stop();
myImage.BackgroundImage = Properties.Resources.myImage;
}
Hope this will help.
Upvotes: 1