Task Scheduling using C# - c#

I want to create one Task Scheduler using C# same as Windows Task Scheduler, to run my .bat (batch) file on particular time.
I found this useful link (http://www.codeproject.com/Articles/38553/TaskScheduler)
in this they schedule trigger, and i want to Schedule my .bat file I mean while i am trying to give my batch file path in tags textbox, its just fired trigger, not run my batch file so, i modify that code little bit, and now I am able to run my batch file also,
but, when i close my application triggering also stop, so, is there any way i can triggering or run my batch file even if i close my application, like window task scheduler???
kindly Help me .
Note: its desktop application using C#

You can programmatically schedule tasks in the Windows Task Scheduler by using this .NET wrapper or by calling the windows command line utility schtasks yourself.

It seems you need a windows service to execute your scheduled tasks. Also see Quartz.NET project, that can be used from smallest apps to large scale enterprise systems.

You can programmatically make a task schedule using Windows Task Scheduler
or you can make a background process or a more recommended approach, a thread so your program still runs in the background and still has a small footprint.

Related

Xamarin.iOS Download Manager

I am working with Xamarin.iOS and implementing a Downloadmanager.
I have found a tutorial on how to implement a DownloadManager in swift, lead by that tutorial I have transcribed the logic written in swift into C#. The DownloadManager works and queues Operations and executes them. The only issue I am facing is the transition of the App to suspended mode. The behavior of the queue is not so deterministic. Sometimes all queued operations are executed and sometimes the app stops the execution.
Use Case 1:
I start the file sync and lock the iPad. Most of the time the queued operations are executed. But sometimes when I unlock the iPad the Application is send to the background and I have to double tap the home button to return to the Application, in this case the file sync has stopped somehow in the middle of execution.
Use Case 2: I start the file sync and send the app to the background. The same behavior like in the previous use case. Sometimes all operations are executed and sometimes the operations stops.
Use Case 3: The customer starts the sync and leaves the iPad unattended after some time he comes back and the Sync is not finished and the Application is in the background. In average about 70% of the sync is done.
For the implementation I have used a "NSOperationQueue" that has "DownloadOperation" as elements. The "DownloadOperation" are just a wrapped of the NSOperation object with the execution set to "Asynchronous".
Possible Questions
I am not sure what happens to the "NSOperationQueue" when the Application changes states?
Is the usage of the "NSOperationQueue" the "right" way to go with the implementation of a DownloadManager?
Are there any common tips for the optimisation of the execution of the "NSOperationQueue" and/or the "NSOperation"
Looking forward to the discussion.
P.S. I have enabled "Background Modes" and "Background Fetch"
According to your description, I suppose you used the NSURLConnection to execute background download which is deprecated. Also, Background Fetch is for small amounts of content and it will be active opportunistically, which is not appropriate for your scenario:
Fetching Small Amounts of Content Opportunistically
Apps that need to check for new content periodically can ask the system to wake them up so that they can initiate a fetch operation for that content.
So, you could switch to use NSURLSession which is officially recommended for downloading content in background. To configure a background session, the download process will be still ongoing in case the app is suspended or terminated(except user has forced quite the app). Refer to this Apple documentation:
Downloading Content in the Background
When downloading files, apps should use an NSURLSession object to start the downloads so that the system can take control of the download process in case the app is suspended or terminated. When you configure an NSURLSession object for background transfers, the system manages those transfers in a separate process and reports status back to your app in the usual way. If your app is terminated while transfers are ongoing, the system continues the transfers in the background and launches your app (as appropriate) when the transfers finish or when one or more tasks need your app’s attention.
...
If tasks have not yet finished and the system terminates your app, the system automatically continues managing the tasks in the background. If the user terminates your app, the system cancels any pending tasks.
About how to create a download manager for background via NSURLSession, you can refer to:
Blog with swift: Downloading files in background with URLSessionDownloadTask
Xamarin guides: Walkthrough - Using Background Transfer Service and NSURLSession
I think you need to download multiple files in background, here's a good guide for you: Downloading multiple files in batches in iOS
.

Creating VS 2010 executable for Daily Tasks

I want to create an executable in VS 2010. This executable will be create an excel spreadsheet and will transfer that file via FTP. I want this executable to be fired off via Windows tasks.
What is the best way to accomplish this? Would I create a regular windows form application, dll, or Empty Project, or windows service?
Thank you in advance for any assistance.
A plain old console application scheduled with the task scheduler should do the trick.
If you need the application to run when a computer is turned on but no one is logged in, create a service. If your application runs only when someone is logged in, but has no UI, use a console application. If your application runs only when someone is logged in and has a UI, use a Winforms app.
I'm not sure what the current best practice is, but in our shop we create console applications and use the task scheduler to execute them.
A library (dll) won't be executable from the task scheduler AFAIK, and a WinForms app isn't very useful for any app that runs automatically (i.e.: doesn't require user interaction).
A service would be appropriate for an application that needs to respond to system events/changes when they occur, which doesn't sound like your use case.

Automated task

I have a C# program that I want to run automatically every night without me having to do this,
I believe there is a way of doing this by setting an automated task however im not sure how to do this?
P.S. not sure if this helps but it has a .exe file in its library.
Thanks in advance.
Have a look at Windows Scheduled Tasks, not sure if it's available only on Windows Server or also XP, Vista, 7...
What about registering this as a scheduled task?
Go to Control Panel -> Scheduled tasks.
Here you can easily add your exe as a task and choose when and how often to run your task.
In Windows 7 this is changed somewhat, but you can find it in
Control Panel -> System and Security -> Administrative Tools -> Task Scheduler
You can schedule tasks to run at whatever interval you like. I don't know what version of Windows you are using but here's how to it in Windows XP
You can use "Scheduled Tasks" on windows.
You could also write your application as a "Windows Service". By this, it would run all the time on a machine, without someone creating a scheduled task.
Windows scheduled tasks is the right way.
Look here for more information.
And here for how to do it in Windows 7

How should I run a background service for creating export files for a web site in the Windows world?

I've created a shiny new ASP.Net MVC site and I have offloaded the 'save as' type of functionality to a helper program that can be run in the background on the server so that the website doesn't need to take all that load.
I created it so that it's easy to run from the command line and right now I have it running from a windows scheduler. The problem is that that has a granularity of 1 minute, which means that some unlucky users of the website will click on the link and get a 59s wait + the time to actually process the report.
I'm also slightly worried about the start up cost of my program. I'm assuming that it would be cheaper to keep the program running constantly. I am worried about the program dying and not being respawned though. With the windows scheduler option at least I don't have to worry about my program bailing so much.
How have you dealt with that?
I'd create a Windows Service and have that running instead of an exe via windows scheduler. You can then set it to auto-start. Then you can just have a timer within the service to poll for work every x seconds.
Another alternative is to use something like MSMQ. Your front end would just insert a message into the queue to represent the work to do, then have a .NET service listening to the queue (you can get it to process messages immediately they appear in the queue, or check the queue manually yourself every x seconds).
Either way, I think a Windows Service is the way to go.

Scheduled .NET code execution in hosted environment?

I've got about 40 lines of .NET code in a console application that read and RSS feed and store information in a database. I need this code to execute every night for as long as the RSS feed exists (indefinitely). Currently, I just launch the console app. from my home computer.
Because I can't trust myself to remember to do this every night, I somehow need to have this code hosted. I'd like to somehow have this app. or code (it could easily be put in an ASP.NET page codebehind and triggered to execute when the page loads) run automatically without me having to run the console app. manually.
Any ideas?
EDIT: I don't want to run this code from my computer; I can't guarantee my computer will not be hibernating or connected to the Internet every night.
EDIT: Right now I'm thinking spawn a background thread using BackgroundWorker in Application_Start of the global.asax, have it download the RSS feed once a day, and Thread.Sleep() the rest of the time.
Although controversial, Jeff Atwood blogged about how they accomplished this for SO using cache expiration. Check it out at https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/.
You could use a similar method and schedule the cache to expire at the time you want the process to kick off.
You could use the Windows built-in task scheduler to start the console application.
I would suggest just using the 'at' command instead of getting this small app hosted somewhere.
C:\>at /?
The AT command schedules commands and programs to run on a computer at
a specified time and date. The Schedule service must be running to use
the AT command.
Email your webhost. Some managed hosts will set up a scheduled task for you.
They will likely have different policies regarding how to call a scheduled task (i.e. some may require it to be an aspx page and not an EXE)
Have you looked at Quartz Scheduler for .NET?
Quoth the tagline: Quartz.NET is a full-featured, open source job scheduling system that can be used from smallest apps to large scale enterprise systems.

Categories