Thread.Sleep() in C#

后端 未结 4 646
孤街浪徒
孤街浪徒 2020-12-06 20:31

I want to make an image viewer in C# Visual Studio 2010 which displays images one by one after seconds:

i = 0;

if (image1.Length > 0) //image1          


        
4条回答
  •  无人及你
    2020-12-06 21:09

    Use a Timer.

    First declare your Timer and set it to tick every second, calling TimerEventProcessor when it ticks.

    static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
    myTimer.Tick += new EventHandler(TimerEventProcessor);
    myTimer.Interval = 1000;
    myTimer.Start();
    

    Your class will need the image1 array and an int variable imageCounter to keep track of the current image accessible to the TimerEventProcessor function.

    var image1[] = ...;
    var imageCounter = 0;
    

    Then write what you want to happen on each tick

    private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
        if (image1 == null || imageCounter >= image1.Length)
            return;
    
        pictureBox1.Image = Image.FromFile(image1[imageCounter++]);
    }
    

    Something like this should work.

提交回复
热议问题