There _has_ to be a better way



  •  Found this today in Code i inherited. Maybe the .Net File Class was too complex... who knows.

        public class bcopy
        {
            public bcopy()
            {
            }
            /
            /// Copies all files with the extension vob in a single tmp File. Name of the file is name of the directory
            /// <param name="directory">The directory.
    
        /// <returns>True if copy exits, false otherwise</returns></pre><pre><returns></returns>        public bool merge_vob_files(string fullfilenames,string targetname)
        {
            string parameter = string.Format("/c copy /b {0} {1} ",fullfilenames,targetname);
            //log.add_log_entry(parameter, "copy");
            Process copy = new Process();
            copy.StartInfo.FileName = "cmd.exe";
            copy.StartInfo.Arguments = parameter;
            copy.StartInfo.CreateNoWindow = true;
            copy.StartInfo.RedirectStandardOutput = true;
            copy.OutputDataReceived += new DataReceivedEventHandler(_copy_feedback);
            copy.ErrorDataReceived += new DataReceivedEventHandler(_copy_feedback);
            copy.StartInfo.RedirectStandardError = true;
            copy.StartInfo.UseShellExecute = false;
            copy.Start();
            copy.BeginOutputReadLine();
            copy.BeginErrorReadLine();
            copy.WaitForExit();
            return copy.HasExited;
        }
        public void _copy_feedback(object sendingProcess, DataReceivedEventArgs outLine)
        {
            //log.add_log_entry(outLine.Data, "copy");
        }
    }
    


  •             copy.WaitForExit();
    return copy.HasExited;

    This is TRWTF.



  • @XIU said:

                copy.WaitForExit();
    return copy.HasExited;

    This is TRWTF.

    I like the way it clevely avoids a race condition, should the child process suddenly spring back to life in between those two lines! :-)

     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.