如何检查timer1是否已启动?

时间:2014-10-22 22:44:47

标签: c# .net winforms

我有DownloadFileCompleted事件:

private void Client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {

            if (e.Error != null)
            {
                timer1.Stop();
                span = new TimeSpan(0, (int)numericUpDown1.Value, 0);
                label21.Text = span.ToString(@"mm\:ss");
                timer3.Start();
            }
            else if (!e.Cancelled)
            {
                timer3.Stop();
                label19.Visible = false;                
                bool fileok = Bad_File_Testing(combinedTemp);
                if (fileok == true)
                {
                    File1 = new Bitmap(combinedTemp);
                    bool compared = ComparingImages(File1);
                    if (compared == false)
                    {

                        DirectoryInfo dir1 = new DirectoryInfo(sf);
                        FileInfo[] fi = dir1.GetFiles("*.gif");
                        last_file = fi[fi.Length - 1].FullName;
                        string lastFileNumber = last_file.Substring(82, 6);
                        int lastNumber = int.Parse(lastFileNumber);
                        lastNumber++;
                        string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
                        identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
                        if (identicalFilesComparison == false)
                        {
                            string newfile = Path.Combine(sf, newFileName);
                            File.Copy(combinedTemp, newfile);
                            LastFileIsEmpty();
                        }
                    }
                }
                else
                {
                    File.Delete(combinedTemp);
                }
                File1.Dispose();
            }
        }

如果e是错误,则timer1停止并且timer3启动。 在timer3中,我试图在30秒后再次下载文件。 然后在下一次它到达已完成的事件时,例如这次e不会显示任何错误,所以它将转到第二部分:

else if (!e.Cancelled)
            {
                timer3.Stop();
                label19.Visible = false;                
                bool fileok = Bad_File_Testing(combinedTemp);
                if (fileok == true)
                {
                    File1 = new Bitmap(combinedTemp);
                    bool compared = ComparingImages(File1);
                    if (compared == false)
                    {

                        DirectoryInfo dir1 = new DirectoryInfo(sf);
                        FileInfo[] fi = dir1.GetFiles("*.gif");
                        last_file = fi[fi.Length - 1].FullName;
                        string lastFileNumber = last_file.Substring(82, 6);
                        int lastNumber = int.Parse(lastFileNumber);
                        lastNumber++;
                        string newFileName = string.Format("radar{0:D6}.gif", lastNumber);
                        identicalFilesComparison = File_Utility.File_Comparison(combinedTemp, last_file);
                        if (identicalFilesComparison == false)
                        {
                            string newfile = Path.Combine(sf, newFileName);
                            File.Copy(combinedTemp, newfile);
                            LastFileIsEmpty();
                        }
                    }
                }
                else
                {
                    File.Delete(combinedTemp);
                }
                File1.Dispose();
            }

但是因为在第一次出现错误并且我这次停止了timer1没有错误所以我想启动timer1但是在构造函数中我仍然启动timer1并且它试图第一次下载文件所以可能有在没有任何错误的情况下,它将始终到达已完成事件的第二部分。

我可以在任何情况下放入已完成的事件timer1.Start();问题是如果timer1已经启动并且没有停止,那么每次重启都会有问题吗?

这就是为什么我想检查一下已经运行的timer1是否再次启动它。

1 个答案:

答案 0 :(得分:0)

您可以查看timer1.Enabled

bool Timer.Enabled

  

如果当前启用了计时器,则为true;否则,错误。默认   是假的