Q: I want to invoke timer in my c# code on button click.It works well in form load event but didn't work in button click I want to invoke timer in my c# code on button click.It works well in form load event but didn't work in button click.Please help me to fix it.Thank's in advance.
My code is Here:
public partial class Form1 : Form
{
    public int TimeTaken;
    public Form1()
    {
        InitializeComponent();
        lblFilesCount.Text = lblFoldersCount.Text = "0";
    }

    private void btnFolderBrowse_Click(object sender, EventArgs e)
    {
        DialogResult result = fbdFoldersFiles.ShowDialog();
        if (result == DialogResult.OK)
        {
            string[] files = Directory.GetFiles(fbdFoldersFiles.SelectedPath);
            MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
        }
        txtFolderPath.Text = fbdFoldersFiles.SelectedPath;
    }
    private void btnDeleteFiles_Click(object sender, EventArgs e)
    {
        timerDelete.Enabled= true;
        string path = fbdFoldersFiles.SelectedPath;
        DirectoryInfo dirFirst = new DirectoryInfo(path);
        foreach (FileInfo file in dirFirst.GetFiles())
        {
            timerDelete.Enabled = true;
            DateTime dt = File.GetLastWriteTime(file.FullName);
            if (dt < dtpDate.Value)
            {
               DeleteFiles(file);
            }
        }
        string[] dirArray = Directory.GetDirectories(path, "*", SearchOption.AllDirectories).ToArray();
        Array.Reverse(dirArray);
        foreach (var directory in dirArray)
        {
            if (Directory.GetFiles(directory).Length > 0 || Directory.GetDirectories(directory).Length>0)
            {
                DirectoryInfo dir = new DirectoryInfo(directory);
                FileInfo[] files = dir.GetFiles().ToArray();
                foreach (FileInfo file in files)
                {
                    timerDelete.Enabled = true;
                    DateTime dt = File.GetLastWriteTime(file.FullName);
                    if (dt < dtpDate.Value)
                    {
                        DeleteFiles(file);
                    }
                }
                if (Directory.GetFiles(directory).Length == 0 )
                {
                    dir.Delete(true);
                    lblFoldersCount.Text = (Convert.ToInt32(lblFoldersCount.Text) + 1).ToString();
                }
            }
        }
        timerDelete.Enabled = false;
    }
    protected void DeleteFiles(FileInfo DeleteFile)
    {            
        dgvDisplayFilesInfo.Rows.Add();
        int RowIndex = dgvDisplayFilesInfo.RowCount - 1;
        DataGridViewRow r = dgvDisplayFilesInfo.Rows[RowIndex];
        r.Cells["dgvColoumnSrNo"].Value = RowIndex+1;
        r.Cells["dgvColumnFullName"].Value = DeleteFile.Name;
        r.Cells["dgvColumnPath"].Value = DeleteFile.Directory;
        DeleteFile.Delete();
        lblFilesCount.Text = (Convert.ToInt32(lblFilesCount.Text)+1).ToString();
    }

    private void timerDelete_Tick(object sender, EventArgs e)
    {
        TimeTaken = TimeTaken++;
        lblShowTimeTaken.Text = TimeTaken.ToString();
    }
}


A: First of all you are setting the Enabled=True in the foreach which I don't think it's what you intended. 2nd of all, why don't you use Stopwatch component. It's much easier to use.
Stopwatch sw = new Stopwatch();
sw.Start();
....
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds.ToString())

