C#定时器的间隔不同

时间:2015-01-07 04:01:11

标签: c# timer

这里我有list[N]个图片,两个图片框,不时显示下一个图片,比如自动播放专辑,间隔时间应该不同,任何人都可以帮忙:)非常感谢< / p>

try
{ 
       for (int index = 0; index < list.Count; index++)
       {
             aTimer = new System.Timers.Timer(10000);
             aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
             aTimer.Interval = Convert.ToInt64(arrTime[index]);
             aTimer.Enabled = true;
             pictureBox1.Image = list[index];
             pictureBox2.Image = list[index+1];
       }
}
catch
{
     MessageBox.Show(e.ToString());
} 

如何处理Timer_Tick(){} ...

2 个答案:

答案 0 :(得分:1)

如果您希望同时更改两个PictureBox,请执行以下操作:

public partial class Form1 : Form
{

    private Random R = new Random();
    private IEnumerator<Image> images;
    private List<Image> list = new List<Image>();
    private System.Windows.Forms.Timer aTimer = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();

        this.Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // ...populate "list" somehow ...
        String PicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        foreach(String PictureFile in System.IO.Directory.GetFiles(PicturesPath, @"*.png"))
        {
            list.Add(new Bitmap(PictureFile));
        }

        aTimer.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        aTimer.Tick += aTimer_Tick;
        aTimer.Start();

        ChangeImages();
    }

    private void ChangeImages()
    {
        pictureBox1.Image = NextImage();
        pictureBox2.Image = NextImage();
    }

    private Image NextImage()
    {
        if (images == null && list.Count > 0)
        {
            images = list.GetEnumerator();
        }
        if (images != null)
        {
            if (!images.MoveNext())
            {
                images.Reset();
                images.MoveNext();
            }
            return images.Current;
        }
        else
        {
            return null;
        }
    }

    private void aTimer_Tick(object sender, EventArgs e)
    {
        // change the Interval:
        aTimer.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        ChangeImages();
    }

}

如果您希望两个PictureBox独立更改,请使用两个Timers并单独更改它们:

public partial class Form1 : Form
{

    private Random R = new Random();
    private IEnumerator<Image> images;
    private List<Image> list = new List<Image>();
    private System.Windows.Forms.Timer aTimer1 = new System.Windows.Forms.Timer();
    private System.Windows.Forms.Timer aTimer2 = new System.Windows.Forms.Timer();

    public Form1()
    {
        InitializeComponent();

        this.Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // ...populate "list" somehow ...
        String PicturesPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        foreach(String PictureFile in System.IO.Directory.GetFiles(PicturesPath, @"*.png"))
        {
            list.Add(new Bitmap(PictureFile));
        }

        aTimer1.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        aTimer1.Tick += aTimer1_Tick;
        aTimer1.Start();

        aTimer2.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        aTimer2.Tick += aTimer2_Tick;
        aTimer2.Start();

        pictureBox1.Image = NextImage();
        pictureBox2.Image = NextImage();
    }

    private Image NextImage()
    {
        if (images == null && list.Count > 0)
        {
            images = list.GetEnumerator();
        }
        if (images != null)
        {
            if (!images.MoveNext())
            {
                images.Reset();
                images.MoveNext();
            }
            return images.Current;
        }
        else
        {
            return null;
        }
    }

    private void aTimer1_Tick(object sender, EventArgs e)
    {
        // change the Interval:
        aTimer1.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        pictureBox1.Image = NextImage();
    }

    private void aTimer2_Tick(object sender, EventArgs e)
    {
        // change the Interval:
        aTimer2.Interval = R.Next(3000, 10001); // 3 to 10 second interval
        pictureBox2.Image = NextImage();
    }

}

答案 1 :(得分:0)

try
        { 
        for (int index = 0; index < fileNum / 2 - 1; index++)
        {
            aTimer = new System.Timers.Timer(10000);
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = Convert.ToInt64(arrTime[index]);
            aTimer.Enabled = true;
            pictureBox1.Image = list[index];
            pictureBox2.Image = list[index+1];
        }
        }
        catch
        {
            MessageBox.Show(e.ToString());
        }