Share via


WaitForExit not Responding

Question

Thursday, November 5, 2020 1:16 PM

Hi Team,

I am developing Windows Service which will generate csv file through batch file.Later I will read the file.

I am able to generate to the csv file when I execute in Console. The Windows Service generates batch file.

But when I execute it as windows Service Process.WaitForExit does not return.

I individually double click the batch file it executes . Below is the code

 Process procstart = new Process();
            ProcessStartInfo psi = new ProcessStartInfo
            {
                CreateNoWindow = true,
                UseShellExecute = false,
                FileName = "cmd.exe",
                Arguments = @"/C "+ BatfileName,
                WindowStyle = ProcessWindowStyle.Hidden,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                ErrorDialog = false
            };

            procstart.StartInfo = psi;
            procstart.Start();
            procstart.WaitForExit();

When I go to location and try to open the file it says FIle is in another process.

When I try to delete the file it says "The action cannot be completed because the file is in open in cmd.exe

All replies (5)

Thursday, November 5, 2020 2:37 PM

WaitForExit is a dangerous method to call. It blocks until the process returns. There are several reasons why a process may not return.

1. The process it generating so many output messages that the output buffer fills up. if the output buffer fills up and you are not processing the output results then the process will freeze until more room is available in the buffer.

2. The process is waiting for user input. In this case the process will never complete.

Note also that you have set the Redirect properties on the start info but I don't see that you're actually redirecting. This will cause problems. If you set these properties to true then you need to be actually redirecting the streams and it doesn't appear you are doing that here.

These problems are well documented so you can google for the various solutions that are available. In most cases simply processing the output messages (and/or error messages) is sufficient. But that has to be done async otherwise you'll still deadlock. Refer to the documentation on how to do this. It will also let you see how far in the processing you are getting and if any errors are occurring (check the error stream as well).

Finally note that you should be able to set UseShellExecute to true and set FileName to the batch file path. There is no reason to explicitly run the command prompt as Windows is already configured to do that.

Michael Taylor http://www.michaeltaylorp3.net


Thursday, November 5, 2020 3:03 PM

Doesn't setting UseShellExecute to true preclude redirecting the child process output? Assuming of course that the OP actually intended to do that.  :)


Thursday, November 5, 2020 3:13 PM

Probably, because UseShellExecute calls ShellExecute under the hood which doesn't expose the streams but I've never tried. Nevertheless setting the redirect properties doesn't make sense unless you're actually redirecting the streams. So you either redirect the streams and process the output/error buffers or use shell exec and don't worry about the streams. Batch/command files tend to behave a little different from Process.Start so I generally avoid using them at all in C#.

Michael Taylor http://www.michaeltaylorp3.net


Thursday, November 5, 2020 4:31 PM

Hi team,

I did the below actions and it seems it worked.

I achieved this by adding exit command in batch file.Also I had to executed the file with my credentials.


Friday, November 6, 2020 1:13 AM

Hi sakulkarni83,
I am glad you have got your solution. We appreciated you shared us your solution. And we also hope you can mark it as an answer. By marking a post as Answered, you help others find the answer faster.
Best Regards,
Daniel Zhang

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].