如何始终运行功能! [英] How run a function for always !

查看:112
本文介绍了如何始终运行功能!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我想永远执行一个函数!我该怎么做?定时器或线程是什么?
Windows服务又如何呢?它会连续执行代码吗?

[更新:更详细的说明]
我认为这很明显.但是我没有描述它.
我写了一个程序,删除目录中的旧文件.但是它应该始终检查该文件夹并删除旧文件,而不是一次.我想知道怎么做.
[/update]

Hi anybody
I want to execute a function for always!.How can i do it?whit timer or thread?
and how about windows service?Is it execute it''s code continuously?

[update: a more detailed explanation]
I think it was obvious .But no problem I describe it .
I wrote a program that delete old files in a directory.But it should always check that folder and delete old file,Not one time.I want to know how it could be done.
[/update]

推荐答案

Windows Service一直在运行(Windows负责此工作)-因此,如果要在特定位置执行代码
Windows Service is running continuously (Windows takes care of that) - so if you want to execute code at certain intervals, it is a good choice.


我编写了一个程序,用于删除目录中的旧文件.但是它应始终检查该文件夹并删除旧文件"

您可能需要Windows服务,但这取决于您是否需要一定程度的用户界面.但是,这里可能涵盖了您尝试执行的操作的机制:在C#.NET中监视文件夹活动 [^ ]

读一读,看看它是否对您有足够的解释.如果没有,请尝试更详细地解释-您给我们的越多,我们就会越有帮助.
"I wrote a program that delete old files in a directory.But it should always check that folder and delete old file"

You possibly want a Windows service, but it depends if you need a user interface to a cartain extent. However, the mechanics of what you are trying to do is probably covered here: Watching Folder Activity in C# .NET[^]

Give it a read, and see if it explains enough to you. If not, try to explain in more detail - the more you give us, the better we can help.


while (true)
{
  function();
}



这肯定会杀死处理器,因为它肯定会尝试尽可能快地运行...那么一个好的建议是在每个循环周期中或使用计时器放置一个sleep()调用...

希望这对您有帮助...



This would kill the processor as for sure it would try to run as fast as possible... then a good recommendation would be to put a sleep() call in each loop cycle or using a timer...

Hope this helps...


这篇关于如何始终运行功能!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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