简体   繁体   中英

progressbar for Process.Start()

I tried so many links on this question but couldn't find any help on it. I have this code where i execute a .exe file which retrieves data from a web page and shows the results on my listbox. This takes time and the window freezes, i decided to use BackgoundWorker but all the examples use a loop to update the progress bar where in my case there is no loop, how can i make my progressbar increment? Please help me on this one, here is my code,

        labelstat.Text = "Please wait..";
        // Start the child process.
        Process p = new Process();


        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = "Out.exe";

        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        checkedListBox1.Items.AddRange(output.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries));

I tried putting this on the,

        backgroundWorker_DoWork

method, where it does my task in the background but i have no idea how to increment the progressbar according to the process carried out by my code? :( thank you very much..

Not sure if you'd be able to change a progress bar, after all how do you know what percentage of work the program has done. What you could do though is keep the UI responsive. You've probably already got that done using a background worker, but you might be able to do more by adding lines to your UI as the program writes to the output stream (although given that it needs to download data you might find no output until that's done then it all at once).

You'd do this by creating a loop that runs until p.HasExisted (indicating the program is finished). In that loop you'd report progress ( BackgroundWorker.ReportProgress ), maybe passing in the output string array so far. Then, in the background worker's progress changed event handler you'd update your UI using the array object you passed in when you reported progress.

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