Prasad Matkar February 2016

C# batch file execution never exits

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

Answers


Luaan February 2016

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.

Post Status

Asked in February 2016
Viewed 3,082 times
Voted 8
Answered 1 times

Search




Leave an answer