Process.WaitForExit() method inconsistent behaviour

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have checked many forums for inconsistent behaviour of Process.WaitForExit() method but I couldnot identify the real issue. I have the below code
<div style="color:Black;background-color:White; <pre>
Process process = <span style="color:Blue; new Process();
process.StartInfo.FileName = AppSettings.BcpExe;
process.StartInfo.Arguments = args;
process.StartInfo.CreateNoWindow = <span style="color:Blue; false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
logger.InfoFormat(<span style="color:#A31515; "Redirecting BCP process standard error of: {0} to log file", _feedName);
process.StartInfo.RedirectStandardError = <span style="color:Blue; true; process.StartInfo.UseShellExecute = <span style="color:Blue; false; process.EnableRaisingEvents = <span style="color:Blue; true;

process.Exited += <span style="color:Blue; new EventHandler(process_Exited);
process.Start();
_stderr = process.StandardError.ReadToEnd
process.WaitForExit();
_exitstate = process.ExitCode;
logger.InfoFormat(<span style="color:#A31515; "Sleeping for: {0} milli secs", BCP_SLEEP);
Thread.Sleep(BCP_SLEEP);

[/code]
Above code invokes BCP.exe to output data into temp file. This file is then read by two threads (reader and writer) to write to another file after some processing on it. process_Exited method is as follows:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; void process_Exited(<span style="color:Blue; object sender, EventArgs e)
{
logger.InfoFormat(<span style="color:#A31515; "Error from BCP Process received (if Any): {0}", _stderr.Trim().ToString());
logger.InfoFormat(<span style="color:#A31515; "Exit State of BCP process: {0}", _exitstate);
logger.InfoFormat(<span style="color:#A31515; "BCP Completed for: {0}", _feedName);

<span style="color:Blue; if (File.Exists(_bcpFileName))
{
_feedWriter = <span style="color:Blue; new FeedWriter(_feedName);
_feedWriter.WriteHeader();

Thread reader = <span style="color:Blue; new Thread(ReaderThread);
Thread writer = <span style="color:Blue; new Thread(WriterThread);
reader.Start();
writer.Start();
reader.Join();
writer.Join();

Thread.Sleep(BCP_SLEEP); <span style="color:Green; //Sleep so that files are flushed
}
<span style="color:Blue; else
{
logger.ErrorFormat(<span style="color:#A31515; "BCP file: {0} NOT found after process completion", _bcpFileName);
Environment.ExitCode = -1;
}
}
[/code]
The issue is that the main thread "SOMETIMES" doesnot wait on process.WaitForExit() method and eventually the main thread sleeps for the time and then the execution flow moves to the main caller. The Reader thread and Writer Thread just abruptly
ends and the file is not processed completely hence the error. Most (95%) of the time this code works perfectly but it is observed that whenever there is load on the server then we find this inconsistent behaviour and hence the error.
I am certain that the BCP process runs completely as we have checked the file created by BCP which is complete. I need to know why there is this inconsistent behaviour? The code is complied on .Net3.5 and the Framework on production is .Net3.5 SP1.
How can this inconsistent behaviour can be solved?<br/>
<br/>
Thanks for your help.

View the full article
 

Similar threads

Back
Top