Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How do i make that the progressBar will start in the exact time with the backgroundWorker and finish will get to the end when the backgroundWorker is finished ?

$
0
0

In general its working but in the end the progressBar is waiting just a liitle bit from the end untill the process will finish and then the progressBar finish the little bit that left .

This is the code of the button click event :

private void Diagnose_Click(object sender, EventArgs e)
        {
            progressBar1.Maximum = 100;
            count = 0;
            countBack = 5;
            CreateZip.Enabled = false;
            ViewLogFile.Enabled = false;
            DriverVerifier.Enabled = false;
            Diagnose.Enabled = false;
            Diagnose.Text = "PROCESSING PLEASE WAIT";
            if (filesContent.Length > 0)
            {
                for (int i = 0; i < filesContent.Length; i++)
                {
                    File.Copy(filesContent[i], Path.Combine(contentDirectory, Path.GetFileName(filesContent[i])),true);
                }
            }
            CreateDriversList();
            backgroundWorker1.RunWorkerAsync();
        }

The function directx information:

private void DirectXInformation()
        {
            Process proc = new Process();
            proc.EnableRaisingEvents = true;
            proc.Exited += new EventHandler(proc_Exited);
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.FileName = "test.bat";
            proc.StartInfo.CreateNoWindow = true;
            proc.Start();
            proc.WaitForExit();
            proc.Close();
        }

The background events:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                backgroundWorker1.ReportProgress(i); //run in back thread
            }
            DirectXInformation();
        }

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar1.Value = progressBar1.Maximum;
            timer1.Enabled = true;
        }

Timer1 tick event:

private void timer1_Tick(object sender, EventArgs e)
        {
            count++;
            Diagnose.Text = "PROCESS HAS FINISHED" + "  " + countBack--;
            if (count == 6)
            {
                Diagnose.Text = "COLLECT INFORMATION";
                Diagnose.Enabled = true;
                CreateZip.Enabled = true;
                ViewLogFile.Enabled = true;
                DriverVerifier.Enabled = true;
                timer1.Enabled = false;
            }
        }

The progressBar start to move once i click the button .

Then the progressBar getting very near to the end and wait untill the DirectXInformation() function to end if im not wrong.

Then the progressBar complete to the end and then the timer counting start.

I wanted that the progressBar will progress according to the tasks i have in the Diagnose Button click event.


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>