2

I am trying to execute a batch file which runs on its own. I am now trying to automate this by deploying it as a windows service which listens for a folder and invokes the batch file using file watcher event. Here is the code -

void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
    ServiceEventLog.WriteEntry(TheServiceName + " Inside fileSystemWatcher_Created() - ");
    if (e.Name.Trim().ToUpper().Contains("FU4DGF_TRADES"))
    {
        try
        {
            Utilities.SendEmail("IAMLDNSMTP", 25, "desmond.quilty@investecmail.com", "IAMITDevelopmentServices@investecmail.com", "Ben.Howard@investecmail.com", "prasad.matkar@investecmail.com", "StatPro BatchFile Execution Started ", "");
            int exitCode;
            //  ProcessStartInfo processInfo;
            ServiceEventLog.WriteEntry(TheServiceName + " Before creation of instance of Batch process - ");
            Process process = new Process();
            process.StartInfo.FileName = @"C:\Program Files (x86)\StatPro Suite\MonthlyUpload.bat";
            process.StartInfo.RedirectStandardOutput = false;
            process.StartInfo.RedirectStandardError = false;
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.WorkingDirectory = @"C:\Program Files (x86)\StatPro Suite";
            process.StartInfo.UseShellExecute = false;
            ServiceEventLog.WriteEntry(TheServiceName + " Before start  of Batch process - ");
            process.Start();
            ServiceEventLog.WriteEntry(TheServiceName + " After start  of Batch process - ");
            process.WaitForExit();
            //while (!process.HasExited)
            //{
            //    System.Threading.Thread.Sleep(100);
            //}

            ServiceEventLog.WriteEntry(TheServiceName + " After process.close - ");
            System.Environment.ExitCode = process.ExitCode;
        }

I can see from my event log that it goes as far as logging - Before start of Batch process. Presumably after that the process starts by invoking process.Start() but then nothing happens. Nothing in the event log, the service is still running i.e. not crashed. No errors. I can see from task manager that it does invoke the exe that it is supposed to invoke via the batch file but the exe simply remains in memory with constant memory and 0 CPU usage suggesting the exe is not doing anything. If I run the batch file manually it works fine. Any idea what could be going wrong?

Simon Karlsson
  • 4,090
  • 22
  • 39
  • 1
    When you run the bat file manually....is there any user prompts are shown? – Viru Feb 08 '16 at 14:46
  • 1
    Possible duplicate of [Executing Batch File in C#](http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp) – MethodMan Feb 08 '16 at 14:50
  • There may be administrative permission issue. please check with that – Balaji Feb 08 '16 at 15:10
  • Since you run a service (that presumably has no deskop access), I think you should set `process.StartInfo.CreateNoWindow = true` instead of `false`. But also check Viru's comment. – René Vogt Feb 08 '16 at 15:30

1 Answers1

1

You disabled UseShellExecute. This means that you can't use the shell to execute the file. bat files are not executables, they are shell scripts.

Since you're not redirecting standard I/O anyway, just enable UseShellExecute and you should be fine.

Luaan
  • 62,244
  • 7
  • 97
  • 116