FoxPro and .NET Interop
Error running VFP exe from C#
Gravatar is a globally recognized avatar based on your email address. Error running VFP exe from C#
  Bob
  All
  Apr 14, 2021 @ 05:01pm

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?

Gravatar is a globally recognized avatar based on your email address. re: Error running VFP exe from C#
  Rick Strahl
  Bob
  Apr 15, 2021 @ 02:15pm

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 ---

Gravatar is a globally recognized avatar based on your email address. re: Error running VFP exe from C#
  Bob
  Rick Strahl
  Apr 15, 2021 @ 04:15pm

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.

Gravatar is a globally recognized avatar based on your email address. re: Error running VFP exe from C#
  Bob
  Rick Strahl
  Apr 16, 2021 @ 07:37am

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.

Gravatar is a globally recognized avatar based on your email address. re: Error running VFP exe from C#
  Bob
  Bob
  Apr 20, 2021 @ 05:48am

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?

Gravatar is a globally recognized avatar based on your email address. re: Error running VFP exe from C#
  Bob
  Bob
  Apr 20, 2021 @ 12:39pm

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.

© 1996-2021