简体   繁体   中英

Batch file not running from c# code

I have a batch file at a shared location which i am executing from the .net code but the batch file is not executing infact what is happening is that the whole code of batch file is not working just partical code is working. I am assuming that the .net code execution is finishing and stopping the execution fo the batch file. Below is my .net code

private static void ExecuteCommand(string command, string arguments)
    {
        try
        {
            Console.WriteLine("Code execution starts");
            Console.WriteLine("command: " + command);
            Console.WriteLine("arguments: " + arguments); 
            var process = new Process();

            process.StartInfo.FileName = command;
            if (!string.IsNullOrEmpty(arguments))
            {
                process.StartInfo.Arguments = arguments;
            }

            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            //process.StartInfo.CreateNoWindow = false;
            //process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.StartInfo.UseShellExecute = true;
            process.EnableRaisingEvents = true;
            //process.StartInfo.RedirectStandardError = true;
            //process.StartInfo.RedirectStandardOutput = true;
            string stdError;
           // process.Start();
            Thread.Sleep(60000);
            ThreadPool.QueueUserWorkItem(delegate
            {
                process.Start();
               // process.WaitForExit();
            });
            //Thread.Sleep(60000);

            //ThreadStart ths = new ThreadStart(delegate()
            //{
            //    process.Start();
            //    //process.BeginOutputReadLine();
            //    //stdError = process.StandardError.ReadToEnd();
            //});

           //process.Start();

           //process.BeginOutputReadLine();
           //stdError = process.StandardError.ReadToEnd();

            Console.WriteLine("Code execution ends");
        }
        catch (Exception e)
        {
            Console.WriteLine("Code execution error: "+e.Message +"||"+e.InnerException.ToString()); 

        }
    }

The requirement is to start the execution of the batch file and do nto wait for any output or finishing of the batch file execution. When i run the batch file from command prompt or windows explorer it is working fine. Can't use cmd.exe in .net code because we need to handle the exceptptions when the batch file is not present or permission issue

I think you could use the WaitForExit method instead of using a Sleep that could or could not be enought.

More info about it: http://msdn.microsoft.com/en-us/library/ty0d8k56.aspx

If that doesn't work, you could use a workaround, like using a file that is created before the process starts and deleted by the batch when it ends, and it's checked periodically by a FileSystemWatcher ( http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx ) as a way to check if the process is completed or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM