让计时器做一次然后停止...... [英] Make timer do something once then stop...

查看:67
本文介绍了让计时器做一次然后停止......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。如何让我的timer1检查桌面上是否有特定扩展名的文件,如果有,我希望timer1启动timer2并执行ONCE操作,然后我希望timer2停止。



这是我的代码到目前为止...你可以看到我有代码检查桌面上是否存在扩展名为.rtf的文件,如果它确实存在,则定时器1将启动计时器2,它将启动一个动作。但正如你现在可能已经理解的那样,我希望动作能够完成一次,然后我希望计时器停止。



 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();

}





我的尝试:



解决方案

嗯,你找到了计时器的Start()方法,难道你不认为可能有Stop()方法吗?您认为应该停止每个计时器,以便它们不会再次点火?


为什么使用计时器进行单一操作,它们没有用处?您所需要的只是:

 私人  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();
}



虽然我怀疑它会失败,因为你的文件列表没有传递给 startAction 方法。


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:

解决方案

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?


Why are you using timers to do single actions, they serve no purpose? All you need is:

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.


这篇关于让计时器做一次然后停止......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆