I am trying to run a C# timer program as a service and call a vfp executable. This is the program
using System;
using System.Timers;
using System.Diagnostics;
namespace CCSWindowsService
{
public class Program
{
private static Timer timer;
public static void Main()
{
timer = new System.Timers.Timer();
timer.Interval = 5000;
timer.Elapsed += OnTimedEvent;
timer.AutoReset = true;
timer.Enabled = true;
}
private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
{
ProcessStartInfo myProcess = new ProcessStartInfo();
myProcess.FileName = @"H:\projects\vetsbeta\ccs_batch.exe"; // Specify exe name.
myProcess.UseShellExecute = false;
Process.Start(myProcess);
}
}
}
This is the error: Is it possible to do what I am attempting to do?
Windows services are not plain Console application, which is what you have in your code.
Frankly for what you're trying to do I think the better approach would be to use a Scheduled Task in Windows to fire the EXE in 5 second (or whatever) intervals.
If you want to build a Windows Service you need to implement the IServiceHost
interface. There are several .NET libraries that provide a wrapper that make this easier.
Here's one called TopShelf
with a tutorial on how to set it up:
https://generalistprogrammer.com/csharp/c-windows-service-tutorial-using-topshelf/
The tool will handle:
- Service installation/uninstallation (via commandline)
- Simple implementation via Start/Stop Methods
which is considerably easier than implementing the raw IServiceHost
interface and dealing with the service registration manually.
+++ Rick ---
Would it be possible to create the scheduled task and manage it (e.g. Pause it to index files) from within VFP code? I am not sure our users would be able to do this on their own. Thanks for the reply.
Yes. I think Doug Hennig has an article about that. I've done it before too, but can't remember what I used that for so can't find my code 😃
Search here and you probably can find Doug's mention of it.
https://doughennig.blogspot.com/2007/04/scheduling-tasks.html
+++ Rick ---
FYI I found a feature that is in Windows 10 and Server 2016 called Schtasks.exe. It appears to let you control most features in Windows Task Handler and can be executed using the Run command in VFP. So far have been able to create a working test. So I am fairly confident I can manage the batch job(s) from my application. I'll let you know how it turns out.
https://docs.microsoft.com/en-us/windows/win32/taskschd/schtasks
I believe you are correct in that this should be an better solution than creating a Windows service.
Task scheduler seemed like a good idea but so far I haven't been able to make it work. The job starts in the scheduler and says it is running. It shows up in task manager with no cpu usage and about 10% of the memory if run outside the task manager. I finally just have to end the task because it is not doing anything. The exe runs fine if started outside the task scheduler. It seems like it should work. Any suggestions?
We finally got it to work. We moved the initiation of the transaction log to the beginning of the code and put in a lot of break points We found a variable used for the log file was getting stepped on Not sure why it worked outside of the tash scheduler Apparently when the task scheduler would encounter an error it would just hang. Oh well, time to clean up the code and truck on.