This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Execute function at specific time
I want to run certain function at certain time.I tried Timer control.it's not working. My question is:How can I run a function every day at the 19:00 in C#?
Is there any way to check the time and have a Timer object?
Timer code:
int Interval(TimeSpan gorevZamani)
{
if ((gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds > 0.0)
return (int)(gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds;
else
return (int)((gorevZamani - DateTime.Now.TimeOfDay).Add(TimeSpan.FromDays(1)).TotalMilliseconds);
}
in the question set as possible duplicate: C# Execute function at specific time people suggest to use either Quartz.NET or windows Task Scheduler.
Both options could eventually serve the purpose but I believe, as I suggested already few times in similar previous questions, Windows Task Scheduler is better because you no code anything for it and let Windows do the scheduling for you and you focus only on the real business case of your application, which is what Windows cannot do for you, then rely on existing technologies to glue things together and don't have to debug or reinvent what has been done and is available for you anyway.
Use a scheduled task. A good way to do this is the at command, documented on MSDN here.
Related
This question already has answers here:
How do I run my c# program as a scheduled task
(10 answers)
Closed 4 years ago.
I have a long-running .exe that I would like executed at 1:30am the morning after a user clicks a button on a webpage. It has two args and will be run infrequently. How do you create a one time scheduled task to run an executable with two args? Thanks!
EDIT: The user clicks a button that enables the .exe to run at 1:30am.
This question is different. I am wanting to know how to CODE the scheduling of a task.
Your can use background scheduler libraries
I would suggest to use Hangifre, its easy to use and can do what you need easily
BackgroundJob.Schedule(
() => your action here,
TimeSpan.FromDays(1));
TimeSpan.FromDays(1) => this you have to calcualte from the time the button is clicked to midnight, and pass the timespan in there, the task will be executed at midnight.
This question already has answers here:
High resolution timer in C#
(5 answers)
Closed 5 years ago.
No matter where I look, I can't find a good answer to this question. I'd like to have something happen at a given BPM (in my example, I'm using BPM), but the basic C# Timer class isn't working for me. Since it only measures in milliseconds, any actions performed within the timer get noticeably unsynced from the music. I've attempted to use this MicroTimer Library but with no luck! Though it can be quite fine grained, it's resource heavy and it doesn't have the resolution necessary. I understand I can have a function with a counter, but is there a good way to do this with Visual Studio's libraries (like the basic timer)? I hear those aren't as processor hungry.
I doubt you'll get the kind of time resolution you're looking for in a managed language like C#.
Hell, even if you were writing in C the OS could decide another process is more important and just like that you're out of sync.
Maybe consider using the timer, but resyncing every second or half second? I'd default to another user if they have experience in this area, but I'd at least give that a shot. Or go by the system clock ticks?
This question already has answers here:
How to perform periodic work on an ASP.NET MVC website?
(4 answers)
Closed 7 years ago.
I have created a service class in a MVC 4.5 project. In this class I need to start a Poll method.
What is the best practice in doing this?
This is a mock up, but it needs to be started, and Thread.Sleep is not recommended. And the method should be global, and only one call for each minute.
private void Poll()
{
Foo("Do somethings");
Thread.Sleep(60000);
Poll();
}
Any good suggestions?
Using your approach will eventually result in a StackOverflowException.
You should leverage the built-in Timer class.
You can find the documentation here.
Your usage code would be something like
Timer t = new Timer(o => Foo("Do somethings"), null, 0, 60000);
Side note:
As #RonBeyer pointed out, perhaps polling in an ASP.net might not be the best tool for the job you need to accomplish.
A WCF long-running service for instance with web-hooks to your ASP.Net application would seem appropriate. Again, it depends on what you need to achieve. Don't hesitate to complete your question with more details about what's your ultimate goal.
This question already has answers here:
How to call a method daily, at specific time, in C#?
(19 answers)
Closed 9 years ago.
I'm in the process of writing a windows service using System.Timers.Timer to keep track of my interval. What I'd like to do is make it so my service will launch on a specific day and time based on variables in my app.config file.
I don't think you're really writing a Windows service, I think you're writing a job. A cleaner, and easier approach, to your problem would be to write a Console application and then setup a Windows Scheduled Task to run that Console application at the intervals you want.
Never mind the fact that you'll have to set the time in milliseconds with the Timer approach, the Windows service would not be capable of handling Daylight Savings Time and more.
Although I think your best bet is to make this a scheduled task, you can easily create a waitable timer that will signal at the same time every day. Windows has a Waitable Timer object, but there's no .NET support for it.
I published an article a while back called Waitable Timers in C#, in which I showed how to use this object from a C# program. Unfortunately, the site that published the article is no longer. However, you can download the code examples for the article from my site at http://www.mischel.com/pubs/waitabletimer.zip. You're free to use the code in any way you see fit.
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What is the correct way to create a single instance application?
What is a good pattern for using a Global Mutex in C#?
Suppose i have created an exe i want that exe must run only once ..how it is possible please give suggestion
If I understand your problem correctly this has nothing to do with having a singleton implementation. You simply need to check if your executable is currently running.
You can do this by calling Process.GetProcesses() or Process.GetProcessesByName(NameOfExecutable) and checking the return values.
Alternatively use a Mutex as suggested above by others.