Click here to Skip to main content
14,838,200 members
Please Sign up or sign in to vote.
2.75/5 (3 votes)
I am a ASP.NET developer and at present I work with C# and SQL Server.

I would like to have an automated process which does the following:-
[1] Connect to SQL Server
[2] Select FTP data (host, username and password) and "time to pull data" from a table
[3] Based on the "time to pull data" (say example 6:00am), it should connect to the FTP server using the selected FTP data
[4] Download all the files available in the FTP server to the local system.
[5] And this process should be executed continuously since, the step [2] will select more than one row with different FTP data and it's corresponding "time to pull data".

Having said the above requirements, my clarification is:-
[1] Should I use Windows Service or Schedule Task
[2] If "Windows Service" then how can the above requirement will be achieved, that is connecting to database, pulling data, connecting FTP as a said point of time, downloading available data to local system and the process round the clock?
[3] If "Schedule Task" then how can I develop modules task for "Schedule Task" using .Net and integrate that module in "Schedule Task".

Your timely guidance with sample code will be highly appreciated.

Mohandas K
Updated 11-Feb-21 9:14am

I would choose a scheduled task for its simplicity anytime. In the earlier days I did use windows services. But time and again I have realized that a scheduled task is pretty simple and straight-forward. Thats because, in the case of a windows service, you need take care of whether its startup mode is automatic or manual and if its manual, you have the pain of starting the service, forgetting which could cause you unwanted issues. Of course you could set it to be automatic, such that the service starts when your machine is restarted.

But you have the overhead of taking care of when to run the service and so on. On the other hand if it's a scheduled task in windows, you need not worry about the startup modes or the windows service related functionality and just concentrate in the actual task of your application.

If you plan to have a scheduled task, you just need to develop a console based application that does what you need. You could also set up the console window to be hidden, so that the console window does not appear when the scheduled time arrives, as it could be annoying to the users (or you) of that system.

PS - This is just my opinion, if you like to differ, please add why as a comment as this could be informative for others too. Thanks!

Hope this helps!
Mohandas_Kulasekaran 19-Apr-11 1:15am
@ Karthik -
Thanks for your reply.
As advised by you I can foresee the issues of using a Windows Service.
Can a console application add tasks to the Schedule Task dynamically without human interference? For example, every time when I add a new record in my table with FTP details and time to pull data, can we add this task corresponding to this record on the schedule task without having to execute the console application every time while adding new record?
Karthik. A 19-Apr-11 1:41am
You need not create a scheduled task from the console application. Its the other way around - you create a scheduled task that runs your console application at the specified intervals. It would be the duty of the console app to access the database get the information on what needs to be done and do it. Refer to this link for an example - A windows scheduled task expects some thing that it could run - it could be anything, say, an executable, batch file, etc etc - with or without parameters.
Mohandas_Kulasekaran 19-Apr-11 2:30am
Thanks for the info Karthik. I am looking for that automation process which will trigger another process to download files through given FTP server and a given point of time. There could be several FTP servers which has to accessed only at a given time relevant to that FTP server. In Task Scheduler, the minimum interval between the repeating a task is 5 mins, whereas there could be some FTP servers which would be required to access within that 5 mins of interval. So the "Task Scheduler", will always run the process atleast few mins delayed than the expected time of execution of my required task.

Thinking out of the box, rather than depending on Task Scheduler, can we develop an windows application, that can run at the background, hiding itself in the task bar just like another application which run hiding and showing only it icon at the right side of task bar beside time / date (like instant messengers, antivirus, etc.,) ? If yes can you please advise me how can it be done ??
shravaniraj 25-Jun-13 10:57am
Hi Mohandas,

I got the same requirement as mentioned above and asked to develop a Windows service. Please let me know, if you got the solution for your question.

Thanks for your time and help.

Mattintosh 25-Jun-13 16:27pm
Spot on. I have written stuff that works exactly this way in the past, and I highly recommend it for unattended batch processing, with a couple of things to be aware of first.

1) Scheduled Tasks (in more recent versions of Windows, it's called Task Manager, and it's under Administrative Tools) will not run more often than every 5 minutes. You set it up to trigger the actions daily at a set time, repeating every 5 minutes thereafter. From what I can tell, you cannot run your task trigger more often than this. If you need to run it with more than a 5 minute granularity, you'll have to use a Windows Service and a threading timer.

2) Building a new command line app for every task you want to trigger is a difficult-to-maintain mess. So you're going to have some overhead with a general-purpose task engine. The ones I've written are specific enough to require an input, archive, and error directory path (or hostname, paths, and credentials for FTP) for each sub-task. The engine checks the database to see which sub-tasks need to run right now, and it kicks them off. The sub-tasks themselves are smart enough to go get their files from the input location and process them, then move them to the archive or error locations (pass/fail style, and we keep a complete history).

So there's a table in the database that tells it when to run, where to get data, where to put data, and what classname to instantiate to process the data. There's a command-line app that gets that config for the currently needed processes, instantiates each active process' class, and runs it. And there's a timer that kicks off the command-line app every 5 minutes.

It works well and it's dirt simple. Let the OS handle the timing stuff. Don't invent your own if you don't have to.
As you are looking to connect to SQL Server to start this process off, I'd actually look to schedule the task inside SQL Server. Have a look here[^] for details on how to create a regularly scheduled task. The beauty of this approach is that you can still do all sorts of clever .NET things inside SQL Server, but you remove the dependencies on other systems.
Member 10754595 17-Jul-17 10:56am
Can you please provide some code how to do it?
Richard Deeming 12-Feb-21 12:13pm
This doesn't add anything to the discussion that wasn't already covered in solution 1, back in 2011.

Resurrecting an old question just to post a link smells like spam. If it wasn't a well-known site and author, then your account would be being kicked about now.

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