Не прорисовывается картинка на форме из-за Sleep - C#
Формулировка задачи:
public Image setImg()
{
Image[] imgArr = new Image[3];
imgArr [0] = WindowsFormsApplication1.Properties.Resources._1;
imgArr [1] = WindowsFormsApplication1.Properties.Resources._2;
imgArr [2] = WindowsFormsApplication1.Properties.Resources._3;
Random rnd = new Random();
r = rnd.Next(0,2);
Image dif = imgArr[r];
return dif;
}
public void runImg()
{
for (int i = 0; i<= 5; i++)
{
Thread.Sleep(1000);
pictureBox1.Image = setImg(); //Тут ничего не происходит
Thread.Sleep(1000);
pictureBox1.Image = WindowsFormsApplication1.Properties.Resources.bg;
MessageBox.Show("piu");
}Решение задачи: «Не прорисовывается картинка на форме из-за Sleep»
textual
Листинг программы
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Timer timer = new Timer();
timer.Interval = 500;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
public void timer_Tick(object sender, EventArgs e)
{
for (int i = 0; i <= 5; i++)
{
pictureBox1.Image = setImg(); //1ый раз
}
}
public Image setImg()
{
Image[] imgArr = new Image[3];
imgArr[0] = WindowsFormsApplication1.Properties.Resources._1;
imgArr[1] = WindowsFormsApplication1.Properties.Resources._2;
imgArr[2] = WindowsFormsApplication1.Properties.Resources._3;
Random rnd = new Random();
Image dif = imgArr[rnd.Next(0, 2)];
return dif;
}
}
}