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.
Related
This question already has answers here:
Detecting whether on UI thread in WPF and Winforms
(12 answers)
Closed 4 years ago.
The title says it all. I would like to be able to simply detect if work is being executed on the UI thread. Also, if the method is not running on the UI/main thread, can I determine what other thread that it might be? I would like to use this information for debugging purposes. Thanks!
In a UWP app, you can check and store the value of the Environment.CurrentManagedThreadId property when your app starts and then compare the current value of the same property with the stored value to determine whether you are on the same thread.
Or use the CoreDispatcher.HasThreadAccess property to determine whether the current thread can safely update the UI.
This question already has answers here:
C# allow simultaneous console input and output
(4 answers)
Closed 5 years ago.
I have a console app that is running multiple worker threads. I would like the console output to have one WriteLine to report the status of each thread. I know I can simply clear, and loop through my threads to report their status on some interval, but at the same time I want to type commands that would be processed by the application using ReadLine(). Is there a common approach to maintain the top x lines of a console to show scrolling or updated info, and have a line below them where I can type in commands and show results of those commands?
That's generally not how consoles work. You could however choose some sort of workarounds as suggested in the answers to this duplicate question: Multithreading: simultaneous console in- and output.
It is suggested there to override contents of the window and/or maintain the input by taking it character by character and redisplaying it.
You could also consider writing a simple non-console application (WinForms, WPF, UWP, etc.) to display a console-like output and input box.
Cheers
This question already has answers here:
Background timer to update UI?
(4 answers)
Closed 9 years ago.
I need to count down time and the ticks must be in synch with time.
First I tried DispatcherTimer. It interacts fine with UI but it lags.I found a lag of almost 5 seconds after two minutes count down.
Then I switched to System.Timers.Timer.This one seems to be more in synch with real time but if I fire events from it which are caught by UI thread I am getting errors.Also inside Timer Elapsed event handler I can't interact with UI elements either.Being WPF amateur my question is how to use it with UI thread to avoid this sort of anomalies?
You can use Dispatcher.BeginInvoke() method. It will schedule delegate to UI thread. Dispatcher is a property of every control.
control.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => { ... } ));
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 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.