Click here to Skip to main content
15,509,458 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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]
Posted
Updated 17-Jul-11 22:14pm
v3
Comments
Sergey Alexandrovich Kryukov 18-Jul-11 3:01am    
Sounds gibberish.
--SA
OriginalGriff 18-Jul-11 3:01am    
Sorry? What are you actually trying to achieve? Do you want to monitor something on a regular basis, or work something out continuously? Or perhaps you are trying to provide information and feel you have to continually look for consumers of your info? They are very different tasks and require different handling.
Use the "Improve question" widget to edit your question and provide better information.

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.
 
Share this answer
 
v2
Comments
sepel 18-Jul-11 3:31am    
Thanks Abhinav ,
First can I set certain intervals for windows service?If not what is this interval?
And does windows runs all of code each time?It means that It is enough for me to place my code in OnStart method?
Abhinav S 18-Jul-11 4:25am    
You will need to use some sort of a timer. OnStart will only run once.
sepel 18-Jul-11 8:35am    
I test it via a windows server and as you say it runs only one time.
Have you any idea that how use timer or thread to it will be run until service stopped?
Espen Harlinn 18-Jul-11 8:38am    
Good reply, my 5
Abhinav S 18-Jul-11 10:03am    
Thank you.
"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.
 
Share this answer
 
Comments
Espen Harlinn 18-Jul-11 8:38am    
Good reply, my 5
while (true)
{
  function();
}


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...
 
Share this answer
 
v2
Comments
sepel 18-Jul-11 23:44pm    
thanks Joan,But when we use a console application or a windows application it may locks the program because it can not exit the loop.How can use a thread to run a function every 1 minutes and return to main program for any user interaction?
Here: http://en.wikipedia.org/wiki/Gibberish[^]. No, this is not bad wording or poor English. You don't really have an idea what are you asking about.

—SA
 
Share this answer
 

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