views:

202

answers:

7

I am in need of some guidance of how to carry out a specific task I need to perform...

I have a database table that contains requests made by users for excel spreadsheets.

In that table there is a status column which indicates the step the request is in.

I need to write a service of some sort that will be able to keep checking the status column of that table.

When it finds a record that contains the status it is looking for, it should pull the data from the record and call another application which creates the excel spreadsheet.

(NOTE: I have written a program that contains a class which creates an excel spreadsheet and saves it to the local hard drive. All of this will be hosted on the same server)

Thanks in advance!

A: 

If I understand your question, then you need to write a loop that periodically checks the status column values for the relevant requests. In pseudocode:

while (true)
     for request r in requests
           <check status column for r, take action as necessary>
     end for
     sleep (some interval)
end while
Glenn
Do you think your pseudo code is very useful?
ChaosPandion
That did it I was just about to mention that you should sleep.
ChaosPandion
And what happens when the machine restarts itself?
Ty
FWIW. If writing a windows service, an infinite loop should not be used - it will eat up way too much CPU. Instead, use System.Threading.Timer to fire off an event.
Loki Stormbringer
@Ty and @Rick: Yes, I was just trying to get at the heart of the matter as I understood it. I think that "How do I write a Windows service?" should be another question.@ChaosPandion: Sorry, I "fat-fingered" a premature response.
Glenn
why use a windows service at all? If most of the work is already written... put the db call into the existing app and have it be a windows scheduled task. no extra resources (or services) needed.
SnOrfus
"How do I write a Windows Service" would have been a better title but I wasn't sure if I should be doing this some other way...please excuse my noobness lol
A: 

I guess my question is...

What is the best way to write a service that checks a database and calls another application. Should I just add the class as a reference in another app that checks the database or should I carry this out in a different way?

Please edit your original post; this site is in a Q-A format, so you shouldn't reply to your own question.
ryeguy
Normally you do this as part of an edit or a comment to your own post.
Ty
He doesn't have enough rep to comment.
shady
sorry..wasnt aware
+2  A: 

You'll want to look at creating a windows service or a process running your checking code set up as a scheduled task.

I am preferential to windows services because they are installed, always start on startup and run in a consistent environment. The scheduled task is dependent on the task not being touched, as well as the executable not being moved.

The main part of the windows service/scheduled task is going to be the timer. Probably every five minutes you will want to check the database table, more or less time depending on the frequency you need.

Here is a decent resource on getting started.

Ty
Ty thanks for your opinion...How would I go about calling the application that actually creates the spreadsheet? How do I configure that application to allow it to be called from a windows service and make sure to keep it running all the time?should I just compile my class into a dll and add it as a reference in the windows service app?
Your code to generate the spreadsheet would be done from the windows service. The service will start up, check the database and then call into your spreadsheet code.
Ty
thanks for the help
A: 

If I were you I would take a dual approach.

I would put all my business 'logic' into a C# library (say widget.core.dll).

Then, in your service application, only have the bare minimum required start and stop the service (i.e. not much more than the standard boilerplate that VS generates), and call directly into your core.dll.

Also write a console app, that takes some input parameters if required, and have this call exactly the same methods in your core.dll as your service. Again, this console app should only have barely enough code to process the arguments and call the core library appropriately. This console app can then be called manually, or as a scheduled task.

By splitting the core library from your implementation of service or console, you will make it a lot easier for you to test your application, as all the acutaly process logic should be in core.dll and callable from unit tests, test code or console app.

With very little extra overhead, you then give a user the flexibility of being able to use the app as either a service or a scheduled task (depending on their preference, environment etc).

service.exe --
             |
             --> core.dll
             |
console.exe--
Jayden
Jayden thanks for your opinion... How would I go about calling the application that actually creates the spreadsheet? How do I configure that application to allow it to be called from a windows service and make sure to keep it running all the time? should I just compile my class into a dll and add it as a reference in the windows service app?
A: 

Ty and Jayden, thanks for your opinions...

How would I go about calling the application that actually creates the spreadsheet? How do I configure that application to allow it to be called from a windows service and make sure to keep it running all the time?

should I just compile my class into a dll and add it as a reference in the windows service app?

If this application could possibly be used elsewhere then I would make it a library (DLL) and reference it from applications (EXEs) that need that functionality. Also, try to keep it generalized by moving things like spreadsheet folder location, name etc to configurations settings that can be retrieved from the app.config.
Loki Stormbringer
thanks for the help
I assume that since you have put this is a comment on all the answers, then you should delete this answer :). (Not meaning to be annoying, but its not actually an answer...)
Lucas Jones
A: 

With reference to your posts:

You could do a couple of things:

  1. You could do what you suggested, have a program (a windows service sounds appropriate) that periodically queries your table (sounds like a select * from tableName where field = something) and then either a: just calls the requisite methods from your app, referenced as a dll in this program.

  2. Include the checking of the table in the program/exe you've already written and just schedule it via the windows scheduled tasks interface.

edit> I think 2 is probably best. Writing a windows service is overkill for this depending on how often the check will happen, and writing all the code to create/manage/restart/whatnot a windows service, though educational, is wasted effort.

SnOrfus
A: 

In reponse to your additional questions in comments :

"How would I go about calling the application that actually creates the spreadsheet?"

I would include the 'application' that creates the spreadsheets in the core.dll (unless you've got a good reason, the fewer libraries the better I think - as it is fewer things to keep track of).

You'll want to have some Public Methods in the DLL that your service or command line app would use.

Most likely a Method to invoke a System.Threading.Timer() object that the service can use. A method that creates the spreadsheet.

Jayden