0

i have a c# console program .Im using .net 2.0 . Im launching an exe from my console program. That exe shows progress bar on taskbar.

Here i want to read progress percentage from that exe and want to display (1% 2% 3% etc) on my console app program.

(i dont want to display whole output of that exe into my program. i also did test of setting following code but it shows all output of that exe i only want to display percentage )

Is this possible using .net 2.0 ?

im beginner in c#

i have following working code

        string fullName = "A.exe" ;
        Process p1 = new Process();
        p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p1.StartInfo.UseShellExecute = true;
        p1.StartInfo.FileName = fullName;
        p1.Start(); 
        p1.WaitForExit();
haseakash
  • 31
  • 1
  • 6
  • p1.StartInfo.UseShellExecute = false; p1.StartInfo.RedirectStandardOutput = true; p1.StartInfo.RedirectStandardError = true; i test with thease code. this code shows all output into my console application. i just want to display 1% 2% 3% done same as exe get finished – haseakash Feb 04 '22 at 19:04
  • Are you sure it's. NET2.0?. NET Framework version 2.0 was released in 2006, and is out of support since 2011.. – Métoule Feb 04 '22 at 22:32
  • 1
    @Métoule : yes. im using net 2.0 – haseakash Feb 05 '22 at 18:35

1 Answers1

1

To read program's progress, you should redirect the standard output and define a method DataReceived that handles OutputDataReceived event of the specified Process instance. This method, for example, gets the percentage of the text, assuming it contains a single number (see GetPercent ), and updates the progress information as you see fit.

static class Program
{
    static void Main(string[] args)
    {
        using (Process proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = "A.exe", // Type filename here.
                WindowStyle = ProcessWindowStyle.Hidden,
                CreateNoWindow = true,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                UseShellExecute = false
            }
        })
        {
            proc.OutputDataReceived += DataReceived;
            proc.Start();
            proc.WaitForExit();
        }
    }

    // Updates a progress value.
    static void DataReceived(object sender, DataReceivedEventArgs e)
    {
        // The e.Data property contains programs output string.
        // Esc-symbol \r means overwriting the current line.
        Console.Write("\rTotal " + GetPercent(e.Data) + " completed.");
    }

    // Gets the percentage from the output string. 
    static string GetPercent(string data)
    {
        // The regular expression that finds a number from a string.
        Regex regex = new Regex(@"([^\d]|^)\d{1,2}([^\d]|$)");
        Match match = regex.Match(data);
        return match.Value;
    }
}

This code should be modified for the specific output format of your program.

ng256
  • 84
  • 2
  • Code works but i cannot see any output on console. console showing black and external exe is running on taskbar – haseakash Feb 06 '22 at 07:54
  • Can you give a sample of the console output when your external exe is run standalone? To hide console window you can search the details about Win API function ShowWindow(IntPtr hWnd, int nCmdShow). – ng256 Feb 06 '22 at 10:12
  • 1st thank you for code. Code works without error. 2nd im using console application. From it im launching A. Exe which is running hidden way but showing taskbar icon and i can see green progress bar on it on taskbar. 3rd my point is. I dont want to hide my console application. I want to Catch A. exe remaining time to get finished and print it on my Console application like. 1% complete 2% complete. I have a another code to hide A. exe on taskbar. Just want to get remaining time of A. exe then calculate it and print on console app.. – haseakash Feb 07 '22 at 11:19
  • Your code didn't give error. But there is no output on console app. Black screen with cursor blinking. There is nothing on console app screen while running. A.exe is still running on taskbar. But this does not printing any output on my console app – haseakash Feb 07 '22 at 11:20
  • Let's go in order. If you toggle a breakpoint on DataReceived and watch out for e.Data, is there any output from A.exe? – ng256 Feb 07 '22 at 18:23
  • I selected proc.OutputDataReceived += DataReceived; toggeled breakpoint. there is no output on console. – haseakash Feb 12 '22 at 18:44
  • CAN U TEST UR CODE WITH jdk-17_windows-x64_bin.exe FILE (RENAME IT TO A.EXE) there is no output on console while A.exe is running (jdk installer). – haseakash Feb 12 '22 at 20:01
  • https://stackoverflow.com/questions/52292942/c-sharp-realtime-standard-output-error-capture-of-a-process – haseakash Feb 13 '22 at 18:54
  • @Albert Zabirov: help – haseakash Feb 13 '22 at 18:55
  • Albert Zabirov answer on following link works for me. its showing live output.while A.exe is running. But it is showing whole output. How to capture and print only 1% done.2% done here with accuracy for exit process?? https://stackoverflow.com/questions/52292942/c-sharp-realtime-standard-output-error-capture-of-a-process – haseakash Feb 13 '22 at 18:57
  • Please help.. somebody – haseakash Feb 24 '22 at 17:51