0

QUESTION: how I can get a process to kill during the middle of the execution of a bat file, I can get it to kill but continues to display result of method.

I'm printing the results to console, however, can't get the results and execution path to stop after kill . I'm running 4 processes.

internal class Program
{
    public static string line = string.Empty;

    private static void Main(string[] args)
    {

        LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(500);
        TaskFactory factory = new TaskFactory(lcts);


        for (var i = 0; i < 4; i++)
        {

            factory.StartNew(() =>
                                 {

                                     var pattern = string.Empty;
                                     ExecuteProcesses(pattern);
                                 }
                );
        }

    Console.ReadKey();
    }




public static void ExecuteProcesses(string pattern)
{

    var startInfo = new ProcessStartInfo();

    startInfo.FileName = @"C:\test.bat"; //pattern to replace string
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardOutput = true;
    startInfo.WorkingDirectory = @"C:\TEST\";
    startInfo.RedirectStandardError = true;
    //startInfo.UserName = "Administrator";


    using (Process batProcess = Process.Start(startInfo))
    {
        Console.WriteLine(string.Format("{0} | START | {1} | {2} | {3}",DateTime.Now, batProcess.StartTime, batProcess.Id, startInfo.FileName));

        //Console.WriteLine(batProcess.s);
        using (StreamReader reader = batProcess.StandardOutput)
        {

            if (!batProcess.WaitForExit(10000))
            {

                Console.WriteLine("Killing");
                try
                {
                    batProcess.CloseMainWindow();
                    batProcess.Kill();
                    batProcess.WaitForExit();
                    Console.WriteLine("Exited");
                    //Console.WriteLine("-->Killing Process ID {0}, Process exit code: {1},  End Time {2}\t",
                      //                batProcess.Id, batProcess.ExitCode, batProcess.ExitTime);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

            ///Console.WriteLine("--> Process ID {0}, Start Time {1}\t", batProcess.Id, batProcess.StartTime);

                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //list.Add(line);
                    Console.WriteLine(line);
                }

            //Write to console
            //Console.WriteLine("----------\r\n" + result + "----------\r\n");
            //Write to log
            //Common.WriteToLog(result);


            Console.WriteLine(result);

            Console.WriteLine(string.Format("{0} |  END  | {1} | {2} | {3}",DateTime.Now, batProcess.ExitTime, batProcess.Id, startInfo.FileName));
            //Console.WriteLine("-->Exit Process ID {0}, Process exit code: {1},  End Time {2}, Total Processing time {3}\t", batProcess.Id, batProcess.ExitCode, batProcess.ExitTime, batProcess.TotalProcessorTime);
        }
    }


    //if (!batProcess.WaitForExit(25000))
            //{
            //    Console.WriteLine("--> Process ID {0}, Process exit code: {1},  End Time {2}\t", batProcess.Id, batProcess.ExitCode, batProcess.ExitTime);
            //    Common.WriteToLog(batProcess.ExitCode.ToString());
            //}
            //else
            //{
            //    Console.WriteLine("Killing, Process exit code: {0}", batProcess.ExitCode);
            //    Process.GetCurrentProcess().Kill();

            //}

        }
    }
Steve
  • 75
  • 1
  • 1
  • 8
  • use CancellationTokenSource class pass it to your startnew as parameter and use it as parameter in your method,for better clarification see this - http://msdn.microsoft.com/en-us/library/dd537607.aspx – terrybozzio Jul 26 '13 at 15:51
  • After using the CancellationTokenSource class and passing the token around, The process continues to return the result even though its been killed and the thread cancelled. Its like the process has run off somewhere and continued to execute. I've checked this in the task manager but the task disappears from the menu once killed. Any idea? – Steve Jul 30 '13 at 14:28

0 Answers0