I've a set of queries which I want to execute only once in day, I know this is possible using TaskScheduler in C#. But I am not getting any example suitable for my requirements. Can anybody give a example code for this?
You can try FluentScheduler. The documentation has the sample codes all you need. Firstly I thought it is for web only, but now I think you can use it for using with Desktop Application too. But not sure and not tested.
https://fluentscheduler.codeplex.com/documentation
EDIT You can also use Task Scheduler -
First create a console application that can run and do all your tasks. You can even invoke other processes with it. Then build the executable and save it in a safe location.
Then go to Administrative Tools > Task Sheduler And create a new task by clicking Action > New Task. You will see a screen like this -
Select your executable and other permissions there.
Now to run it in schedule move to next tab 'Triggers' and click add at the bottom. You will see a screen like this -
Now add your desired schedules. Make sure you use logs, because you will not be able to see the outputs directly. Either you can use windows event viewer or write to custom text file for your convenience.
Task Scheduler is a part of windows itself. It does not have a
dependency on C# or C++ anything. Basically you tell windows that it
will run the specific program at a regular schedule. It is the job of the
executed program to initialize all environment and execute appropriate
code. So even if you use task scheduler you have to make sure that the
program you are using to run with it, has all other options and codes
right.
The Timer is probably the best solution for you.
var timer = new Timer {AutoReset = true, Interval = 30000}; 1s = 1000ms
timer.Elapsed += timer_Elapsed;
timer.Start();
.......
public void timer_Elapsed(object source, System.Timers.ElapsedEventArgs e)
{
// do stuff here that will execute every 30 seconds
}
If you need a reliable scheduler, writing your own from scratch might take more effort than expected. What if the machine gets rebooted? What if it happens 10 seconds before execution time? Should the task be executed late or not at all? Where will the data be persisted? You have to think about all these things.
Alternatively, you could use Quartz.NET. It is a C# port of popular Java job scheduling framework. The codebase is well tested and robust. Have a look at it here:
http://www.quartz-scheduler.net/
Related
What i want to achieve is to loop my code infinite times in my GUI/visual C# as i am reading information through a USB cable.
I have successful read the code but it only shows one reading every time i click the button. I understand that if i want my code to run infinitely, i will need to loop it.
I tried the code below but my program still hangs
while(true)
{
//loop
System.Threading.Thread.Sleep(10000);
}
I found this post and wanted to try the timer method however, i do not know where to put this code.
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=60000;//one minute
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
I am using Microsoft Visual studio 2010, anyone can guide me in using this code?
Please forget about creating such loops in your program, these will kill the UI thread that is actually running your UI, the result will be always bad!
You can run that timer, your code is missing the implementation of timer1_Tick, inside that function, run the code that you want. In your current configuration above, this timer will run every one minute and executes whatever is in timer1_Tick. This is a good Example.
Another option would be to create a thread that does the work for reading the USB and let the UI thread alone. I assume this is a harder option to implement for your so go with the timer option.
I am a newbie to programming So I need lot of support from my friends I am creating a windows forms application and I need that program to run automatically once a day and need to close itself after 2 minutes of the execution how to do it....
I need that program to run automatically once a day
You can use Windows Task Scheduler.
need to close itself after 2 minutes of the execution
You can implement in your application using many different approaches.
For instance, since you write WinForms applications, you can use Timer.
I would like to take the below approach.
Write console application. and add appropriate log message so that you can understand how it goes. if the application need any parameter then pass it from command line or put in a file so that the app can read the file and run automatically.
Add the app to Windows schedule task and monitor log regularly.
if the input parameter changes everyday then just update the input file when necessary.
Hope it will give you a idea.
Thanks.
Ruhul
I have read many articles on .Net 4.0 threading. I have yet to understand how it will apply to my situation.
I have a .Net 4.0 application that is a chat bot for a game. It listens to users that can interface with the bot by typing an ! and the command. For Example: !online will run the code that will return the list that the users are online.
This is a plug in system, so when a command is detected, every plugin has a public void OnFirehandleCommand(object botArgs) available. Inside the code, I can determine if that specific command is for that plugin or not. Then the code is executed.
The problem in the past is that the application will not process another command until the code for that command is finished. Some commands may take anywhere from a few seconds to 4 minutes to finish.
In order to remedy that, I attempted to use multithreading at the entry point where a command is requested by the user. I am using the following code:
botArgs = new BotArgs {Bot = bot, Args = args};
var thread = new Thread(OnFirehandleCommand);
thread.Start(botArgs);
That is the extent of the threading that I use. This fires each time a command is given. However, this seems to be lacking any control at all, and as you see a new thread is generated each time a command is received.
Is there a better way that I can use multithreading for this situation? Are there any examples for a situation like this?
Try tasks :
var task = System.Threading.Tasks.Task.Factory.StartNew(
() => OnFirehandleCommand(botArgs));
I am still pretty much new to c# so you will have to bear with me.
I have developed a windows form program which updates some SQL records as an end of day process for one of our clients.
The next step is that I need to install the program on a server and simulate a button click in the program to become a scheduled task.
I know how to setup the task on the server side where you start program and enter the arguments. But I am unsure as to what code I need to include in my program to achieve this.
Consider using Windows Task Scheduler.
You could extract your business logic to a separate DLL and write a simple Console app that will just run your task after accepting the parameters through command line.
My recommendation would be to get away from running a GUI-based/windowed application from a scheduled task - this is generally madness in practice. Ideally, deploy a console-based version of your application that requires execution (perhaps with parameter arguments) and doesn't require any user (or quasi-user-) interaction.
If you simply can't create a 'system version' of your application, then I guess you have two choices, both immensely ugly: 1) create some kind of macro script which is executed instead of your program, this script could execute the program and issue 'the click', 2) perform 'the click' on startup of your application by invoking the button click handler (maybe based on a parameter to give it a duality in execution modes.)
I think you are also asking about command-line argument passing. See the answers to this question.
In particular, I highly recommend the accepted answer: NDesk.Options.
I have similar task to do making winforms as windows task. what i did is
in windows task scheduler in the task tab,under Run put your exe and then /Auto,it will run as schedule.
Example:winform.exe /Auto
If I'm understanding your question correctly, this is how you could possibly proceed:
Best way to parse command line arguments in C#? -> check the answers and choose a library to process the args or write your own code to do so.
Create a scheduled task if those arguments are present by Creating Scheduled Tasks
If it is a windows application, just go to the bin folder, get the executable file, and finally schedule a task for it by using windows schedule task and choose the exe file as you targeted application.
if it is web application, you may want to include your code in a quartz.net scheduled job, details are on quartz.net website.
Very popular solution is Quartz.NET http://quartznet.sourceforge.net/
Take a look in the Timer class
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
Why not extract your database update logic as a windows service
you can segregate the sql handling part in a separate DLL and use the common DLL for both your form application and the windows service.
A window service run in background and can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface.
Moreover you need not to install any third party software for same and window service code base can be ported to any windows machine with required version of .Net Framework installed.
Add reference: Microsoft.Win32.TaskScheduler
then write this code:
using (TaskService ts = new TaskService())
Microsoft.Win32.TaskScheduler.Task task = ts.GetTask(TaskName);
task.Run(); //start
task.Stop(); //End
I have a script I wrote in C# (part of a ASP.NET application) that would reset a number if the end date of a booking is reached. I would like to automate the process, so each time an end date of booking is reached, it will send an email notifying that period is expired and also execute my script to reset the counter.
I heard that Windows Scheduler could be used? I know cron is perfect for this job, but unfortunately it is windows of course I am using. Any hints on what to do for automation of tasks above would be greatly appreciated.
Go to:
Control Panel->Administrative Tools->Task Scheduler
Create a new task and specify date, time, frequency, etc.
Your C# program can be invoked from the task you created on the step above.
Usually, I either write a small C# console application or use C# Script for this. If I would understand the "strange" (IMHO) syntax of Powershell, this would be the preferable way to do.
No matter what you use, you usually cannot use C# code that is included inside an ASP.NET application. Instead, extract the code into one of the possibilities above and use the Task Scheduler to create a task with the following options:
Depending on your requirements, the scheduled task should be created to run with an administrative user.
Depending on your requirements, the scheduled task should have the “Run with highest privileges” checkbox set.
You could schedule the task to e.g. run every n minutes/hours, even if no user is logged in.