Background C# Service

  • Thread starter Thread starter Tim
  • Start date Start date
T

Tim

I need to create a C# service that will run a stored procedure which
returns a result set. From there I need to create a file using the
result set in a specific format. The format could be fixed length or
comma delimited, or even xml. This service should run in the
background and only execute on the first of the month. If you have
done this or know how to get this done, I would really appreciate the
knowledge.

Break Down of my Problem.
1.) How to create a windows Service in C#?

2.) How do you set the execution time of the service (once a month)?

2.) Format the data in a datatable to meet at client's required file
format?

3.) How do you copy a DataTable into a file that is comma delimited or
fixed length?
 
Tim,

A Windows Service is just a type of Visual Studio project like Windows Forms
or Console Application. It's usually best in my experience to create a
console app first, to test things, and when you're satisfied everything
works, paste the code into a windows service project for your final test.

Creating an installer for a service is probably the gnarliest part of the
process, and frankly, since it's been a year or so since I've had to do it,
I don't remember the details. There is a command line utility that has to
be executed along the way, as I recall. Once the service is up and running
all you really have to do to update it is stop it in the services console,
copy in the updated binaries, and restart the service.

On the other hand ... if this only has to execute once a month, maybe it's
simpler to leave it as a console app and just use Windows Scheduler to fire
it off once a month. I would. Otherwise it's taking up memory and checking
the system date waiting for something to do. You can set a console app to
run minimized, and the user would hardly notice. I imagine you could even
create a WinForms app and hide it in the system tray.

There is no native support for copying a DataTable to a comma-delimited or
fixed-length file. However it's fairly easy to loop through each field and
construct an output record with a StringBuilder and then write it to disk.
Another possiblity is to use a desktop ODBC driver that creates CSV files --
there's one floating around out there someplace I think.

--Bob
 
just one question:

Bob Grommes said:
On the other hand ... if this only has to execute once a month, maybe it's
simpler to leave it as a console app and just use Windows Scheduler to fire
it off once a month. I would. Otherwise it's taking up memory and checking
the system date waiting for something to do. You can set a console app to
run minimized, and the user would hardly notice. I imagine you could even
create a WinForms app and hide it in the system tray.

I'm not sure what you mean by setting up a console app that runs minimized.
If it is a console app that doesn't write to the console, it won't need to
run minimized. It just runs in the background. Output can go to a log
file or just send messages to the application log so that an administrator
can check to make sure that it started and finished.

Am I missing something?

--- Nick
 
If you are already in a console window, a console app that doesn't write to
the conole runs silently and then the command prompt comes back. When you
launch a console app from Explorer, though, the console window appears
whether or not you write anything to it AFAIK. But in Scheduler you can set
a program to run minimized.

I suppose that one could hook one of those Console classes floating around
out there that let you do cursor positioning and set color, font and size --
they might have an option to just plain hide the window. Or is there some
easier way that I don't know about?

--Bob
 
Back
Top