Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading new image each second

I need to load each second (or two) new image.

Following code doesn't work:

System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect2-1.gif"));
System.Threading.Thread.Sleep(2000);
this.Image_LoadImage.Source = new BitmapImage(new Uri(@"D:\\connect3-1.gif"));

What I see is that the app sleeps for 4 seconds and then the second image appears.

How can i do it? Thanks.

like image 987
N.D Avatar asked Jul 16 '26 11:07

N.D


2 Answers

Use timer for this

    private System.Threading.Timer timer;
    public MainWindow()
    {
        InitializeComponent();
        timer = new System.Threading.Timer(OnTimerEllapsed, new object(), 0, 2000);
    }

    private void OnTimerEllapsed(object state)
    {
        if (!this.Dispatcher.CheckAccess())
        {
            this.Dispatcher.Invoke(new Action(LoadImages));
        }
    }

    private bool switcher;
    private void LoadImages()
    {
        string stringUri = switcher ? @"D:\\connect2-1.gif" :
                                      @"D:\\connect3-1.gif";
        this.Image_LoadImage.Source = new BitmapImage(new Uri(stringUri));

        switcher = !switcher;
    }
like image 138
Stecya Avatar answered Jul 18 '26 01:07

Stecya


Use a timer.

Calling thread sleep is gonna block the UI thread. Found this link for you:

like image 25
EKS Avatar answered Jul 18 '26 01:07

EKS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!