private void aMethod()
{
aTimer = new System.Timers.Timer(3000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimerEvent);
aTimer.Enabled = true;
aTimer.Start();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
fileEntries = Directory.GetFiles(@"C:\Users\John\Documents\Visual Studio 2010\Projects\ComeOn\ComeOn\bin\Debug\come");
aMethod();
index = 0;
}
private void OnTimerEvent(Object sender, ElapsedEventArgs e)
{
Bitmap LogoImg = new Bitmap(fileEntries[index]);
LogoImg.MakeTransparent(LogoImg.GetPixel(1, 1));
this.Dispatcher.Invoke(
new Action(() => image1.Source = GetBitmapSource(LogoImg)));
index++;
}
fileEntries的長度是3.我創建了一個計時器,它將在3秒開始。首先它會執行image1.Source = GetBitmapSource(LogoImg)//對於fileEntries [0] 3秒鐘,然後對於fileEntries [1] 3秒鐘和結束fileEntries [2] 3秒鐘。c#計時器奇怪的行爲
但是,我的程序做這個:
啓動計時器,運行fileEntries [0],fileEntries [1]和fileEntries [2]0.05秒,然後等待3秒,然後重新開始。爲什麼是這樣?
'index'聲明和初始化在哪裏? – ChrisF
您是否嘗試過設置aTimer.AutoReset = false? –
@Lucasus是的,它沒有幫助。 –