Subject: schedule C-Sharp process
Posted By: Derf Post Date: 4/16/2008 12:11:39 PM
Greetings to everyone.

I have a asp.net web application written in C#. There are several areas of the application that I wish to have in a stand alone process that will be run with a scheduler.

I know how to create a cron job with php/mysql, is it possible to do this with C# code?  Write a C-Sharp script that is called and all it does is runs, similar to using php.exe to run a php file by specifying the file path. No output is required as it is DB updating and moving files form one folder to another every minute.

Must I create a separate application and create a EXE from it using my working C# code to run in this fashion?

Thanks to anyone with some insight.

Reply By: planoie Reply Date: 4/16/2008 1:19:32 PM
There is no such thing as a C# "script".  C# must be compiled into an executable assembly whether it's a DLL or a EXE by itself.

Typically the requirement you describe can be accomplished by creating a command line executable that performs the desired task, and that exe is set up in the windows scheduler.  I tend to stay away from the windows scheduler as it has poor tracking capabilities and I've found it fails to regularly for my liking.

Given that you are doing DB updating, is there a reason it needs to be in C#?  Can the DB updates be handled by SQL code?  If so, then you can do it all with a SQL job (assuming the DB system you are using supports such jobs).

-Peter
peterlanoie.blog
Reply By: Derf Reply Date: 4/16/2008 1:55:59 PM
The process is as follows...

VoxWare is being used by pickers on the warehouse floor.

If they are in need of X amount of a product for an order they voice in they are short.

The VoxWare will fire off a fixed length flat file to a special directory. These files end in ".REP".

I have the code to slurp up any files that end in .REP and read in the data, update it in the DB for the replenish guys and than transfer the now defunct file to the archives.

I want to make this file so it can be run every minute and not be a page that has to be surfed to in a browser (how it works right now).

There are other processes that must be done in a similar fashion. I have test written a few exe files and made sure they will work (as in they compile and run correctly), but was looking for a way to schedule them to run similar to how Cron jobs run.

Cron simply calls the php file through php.exe. There fore there are no requirements to "surf" to the appropriate php page and there ins't this annoying command window that pops open for a second before self closing (I actually can let that be, just find it vulgar).

So I have an idea on how to accomplish this, but was hoping for some insight into how to cleanly perform this. I have a exe file that will work but when run it pops up an ugly blank command window than closes every time it is run. No one will normally be on this machine that will run the code, I just hate such vulgar processes and was hoping for a clean way.

Thanks.

Reply By: dparsons Reply Date: 4/16/2008 2:02:52 PM
Hmm.  Doesn't seem like this is well suited as an ASP.NET application at all (I bring up ASP.NET here since you are using PHP in your example, although it appears as though you are writing C# Windows apps).  Have you considered writing a Windows Service that implements FileSystemWatcher?  I think this may be your best bet.  

Personally, within my company, we wrote a service that works similar to this where something is done on a website and some value is then wrote to a database. Next, at set intervals, this service executes, reads the new information from the database and then completes the necessary action.

I think something like this is better suited for what you are trying to do but and it solves the problem of not spawing a command window.  However, if you don't wish to use a windows service, this SERP has plenty of links on how to schedule tasks: http://www.google.com/search?source=ig&hl=en&rlz=1G1GGLQ_ENUS249&q=Windows+-+schedule+tasks

Finally, if you want to supress the Console window completely you are probably going to have use P/Invoke to do this.   This SERP should help you: http://www.google.com/search?hl=en&rlz=1G1GGLQ_ENUS249&q=C%23+-+Hide+command+window

-Doug

===========================================================
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
===========================================================
.: Wrox Technical Editor / Author :.
Wrox Books 24 x 7
===========================================================
Reply By: planoie Reply Date: 4/16/2008 3:45:33 PM
I'd go with just what Doug said.  Windows service with a file system watcher.  It will eliminate the annoying pop up command window and will be more efficient because it will remain idle until files appear.

-Peter
peterlanoie.blog
Reply By: Derf Reply Date: 4/18/2008 10:51:05 AM
Ok, question.

Can I place my code in the Elapsed event of Timer or must it always call to some outside process like so many examples show?

Reply By: planoie Reply Date: 4/18/2008 11:15:49 AM
You can certainly put any code in the timer event handlers.  However, generally you'd just call out to some code because you'll probably want to be able to test the code from a process that doesn't rely on the timer or even the process that hosts the timer (perhaps a windows service).

-Peter
peterlanoie.blog
Reply By: Derf Reply Date: 4/18/2008 12:41:41 PM
So your saying to write the windows service that has a timer (and of course install it), so that on every 60 seconds (or 60,000 miliseconds) have the Elapse time event handler call a .exe that is a compiled version of my code that will suck in files and process the data, than move those files to the archives?


That is the direction I am going. Is this what your thinking is?


As to the FileSystemWatcher class, nice features, but I have found it to be redundant as 99% of its features will not be used, so I will stick with the just go through the directory on every process call whether there is going to be files or not.

Reply By: gbianchi Reply Date: 4/18/2008 12:47:17 PM
Just a quick note. Take in mind that if you do something like you are planning, you will have to stop the timer and reenable it after the .exe has finished working. Why? Because you can have a problem somewhere that will slow the .exe and make it last more than a minute, and if you launch the .exe again, you will have 2 programs compiting for the same resources and it can lead to "POTENTIALS" problems (maybe not, but who knows?).

HTH

Gonzalo

===========================================================
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
===========================================================
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
===========================================================
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles/Classics-Week-I-Hate-You.aspx
===========================================================
Reply By: planoie Reply Date: 4/18/2008 10:21:53 PM
Derf,

I'm not sure why you wouldn't use a class that will do just what you need.  Yes, the FileSystemWatcher class may contain many features that you aren't going to use, but that could be said for practically every class in the framework.  Why over complicate your program with your own concoction of logic when the framework provides tested behavior that is exactly what you need?  I would argue that while the built in class may contain features that you won't use, whatever overhead you feel is being taken up by that is far outweighed by the inefficiency of an app that just polls the disk for changes every minute.

Also, I wouldn't recommend simply having a server call another executable.  Put your primary logic in a class library assembly then you can build a standard command line app for testing and the service for the live deployment.  You should be able to put all the monitoring functionality into the main logic assembly.  Then the command line app and the windows service will just be execution stubs for the main logic.  This is the model I've seen used and one I've implemented myself with success.  Having a service exe call another exe will prove quit difficult to debug once the app is running "in the wild".

The other thing I'd recommend strongly is that you implement very strong logging in the app.  I've used log4net with great success.  It allows for good control over the level and output options of logging, of particular importance once you start getting into service apps.

-Peter
peterlanoie.blog

Go to topic 70716

Return to index page 1