none
Progress Bar change value while Background Worker do work RRS feed

  • Question

  • Hello.

    I have an issue. After long search over the internet i have no answer on my problem. Maybe you can help me. I have created an application that has 30MB Size. I have created a program that copy 6 this application from his original location and past it in other folder with diffrent name each time. It takes about a minute to do. I want a Progress Bar value increase each time it is copying the file. My code until now is:

             

    Start Button Click:
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    ==============================================================
    BackgroundWorker1_DoWork:

            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", "C:\Users\New Location\Mini.exe")
            My.Computer.FileSystem.CopyFile(("C:\Users\Mini.exe", C:\Users\New Location\Mini - 1.exe")
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 2.exe")
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 3.exe")
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 4.exe")
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 5.exe")

        End Sub
    ================================================================

    BackgroundWorker1_ProgressChanged:
            Me.ProgressBar1.Value = e.ProgressPercentage

    End Sub
    ===============================================================
    BackgroundWorker1_RunWorkerCompleted:
            MsgBox("All iPhones has been created", vbOKOnly, "Done!")
        End Sub
    ================================================================

    I thought replacing the line in "BackgroundWorker1_ProgressChanged:" from "Me.ProgressBar1.Value = e.ProgressPercentage" to 

    "ProgressBar1.value = ProgressBar1.value + 16.66666666666667" with the idea that every time the backgroundworker do work it will increase by 16.66666666666667 until  reach the 100 percent after 6 Possess. Because it has to do 6 progresses so 16.66666666666667 * 6 = 100. So when it finish the hole proccess the progress bar will reach the 100%. But nothing happens with this code.

    Can anyone help me?

    Thank you very much!!!!!!





    Friday, August 31, 2012 12:17 PM

Answers

  • Hi Andrikos, 

    You need to call ReportProgress() in BackgroundWorker1_DoWork as:

    BackgroundWorker1_DoWork:
            
    int totalNoOfFiles=5;
    int count=0;
    
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
    
    My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", "C:\Users\New Location\Mini.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile(("C:\Users\Mini.exe", C:\Users\New Location\Mini - 1.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 2.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 3.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 4.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 5.exe")
    
    
        End Sub

    If possible, convert the above code into for loop


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, August 31, 2012 4:30 PM

All replies

  • Hello Andrikos,

    You have to set the WorkerReportsProgress property of your BackgroundWorker to True.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.workerreportsprogress.aspx

    Friday, August 31, 2012 12:21 PM
  • After setting WorkerReportsProgress, you have to call ReportProgress whenever you want to fire ProgressChanged.
    Friday, August 31, 2012 12:26 PM
  • Hi Andrikos, 

    You need to call ReportProgress() in BackgroundWorker1_DoWork as:

    BackgroundWorker1_DoWork:
            
    int totalNoOfFiles=5;
    int count=0;
    
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
    
    My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", "C:\Users\New Location\Mini.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile(("C:\Users\Mini.exe", C:\Users\New Location\Mini - 1.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 2.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 3.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 4.exe")
    
    count=count + 1
    
    BackgroundWorker1.ReportProgress(CInt((count / totalNoOfFiles) * 100))
            My.Computer.FileSystem.CopyFile("C:\Users\Mini.exe", C:\Users\New Location\Mini - 5.exe")
    
    
        End Sub

    If possible, convert the above code into for loop


    Regards, http://www.shwetalodha.blogspot.in/

    Friday, August 31, 2012 4:30 PM
  • Dear Steve Hémond and JohnWein. Thank you very much for your useful tips and quick reply.

    Dear Shweta Jain, thank you very much for your code that you provide. It helped me understand, what i had to do and it worked perfectly.

    Thank you very much, for all the help. It is great to know that there are people out there who wants and can help other people. Thank you again!!!!!

    Monday, September 3, 2012 4:39 PM