-1

I am new to C#, I have to start a very time consuming process from my C# program of course without bearing the loss of ui freeze, also I want to read the output printed by the program in cmd and at last I want a stop button so that I can close the program whenever I want...

Please help..

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

try:

using System.Diagnostics;

void startProcess()
{
Process p = new Process();
            p.StartInfo.FileName = "FileName";
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
            p.Start();

            var output = p.StandardOutput.ReadToEnd();
}

MethodInvoker starter = new MethodInvoker(startProcess);

starter.BeginInvoke(null, null);

for ending the process:

p.close()
kuso.sama
  • 336
  • 1
  • 9
0

Use something like this:

void StartProcess(){
   Process p = new Process();
   p.StartInfo.FileName = "yourfile.exe";
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.RedirectStandardOutput = true;
   p.Start();
   var readingThread = new System.Threading.Thread(() => {
      while (!p.StandardOutput.EndOfStream){
         Console.WriteLine(p.StandartOutput.ReadLine());
         System.Threading.Thread.Sleep(1);
      }
   }
   readingThread.Start();
}
Paul Sütterlin
  • 334
  • 1
  • 6