Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. How do I make my timer1 check if there are files with a certain extension on the desktop, and if there are, I want timer1 to start timer2 and do an action ONCE, then I want the timer2 to stop.

Here is my code so far... as you can see I have the code for checking if files with the extensions ".rtf" exist on the desktop, and if it does exist timer 1 will start timer 2 which will start an action. But as you may have understood by now, I want the action to be done once, then I want the timer to stop.

private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
        }
private void timer1_Tick(object sender, EventArgs e)
        {
            string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.rtf");
if (files.Length > 0)
    {
        timer2.Start();
    }

        }
        private void timer2_Tick(object sender, EventArgs e)
        {
           startAction();
                                
        }


What I have tried:

Posted
Updated 14-Aug-18 21:24pm
v5

Well, you found the Start() method of the timer, don't you think there may be a Stop() method? Where do you think you should be stopping each timer so they don't fire again?
 
Share this answer
 
Comments
BillWoodruff 14-Aug-18 23:36pm    
+5 for use of Socratic method !
Why are you using timers to do single actions, they serve no purpose? All you need is:
C#
private void Form1_Load(object sender, EventArgs e)
{
    timer1.Start();
}

private void timer1_Tick(object sender, EventArgs e)
{
    string[] files = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "*.rtf");
    if (files.Length > 0)
        startAction();  
}

Although, I suspect it will fail, as your files list is not being passed to the startAction method.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900