locked
What am I missing ? RRS feed

  • Question

  • Hello all,

    I am trying to create my own maintenance program, but i am doing something wrong.

    The progress-bar will not show properly show progress, it is empty and suddenly it is full.

    But when i try to go on, i get an warning the background worker is still busy.

    here is the code I'm using

    Imports System.IO
    
    Public Class MGS___Bibliotheek
    
    #Region " Backgroundwerkers "
        '
        Private WithEvents Te_Bestand_Werker As New System.ComponentModel.BackgroundWorker()
        Private WithEvents IE_Cookies_Werker As New System.ComponentModel.BackgroundWorker()
        Private WithEvents IE_Bestand_Werker As New System.ComponentModel.BackgroundWorker()
        '   Tellers om te zien hoeveel keer er iets wordt verwijdert
        Private TEMP_Bestanden_Teller As Integer = 0
        Private IE_Bestand_Teller As Integer = 0
        Private IE_Cookies_Teller As Integer = 0
        '
    #Region " Tijdelijke Bestanden Folder - Achtergrond werker "
        '
        Private Sub Te_Bestand_Werker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles Te_Bestand_Werker.DoWork
            '
            '   Waar moet er werk worden gedaan.
            Dim Tijdelijke_Bestanden As String = "C:\Users\" + Environment.UserName + "\AppData\Local\Temp"
            For Each gevonden_bestand As String In My.Computer.FileSystem.GetFiles(Tijdelijke_Bestanden, FileIO.SearchOption.SearchAllSubDirectories)
                Try
                    File.Delete(gevonden_bestand)
                    TEMP_Bestanden_Teller = TEMP_Bestanden_Teller + 1
                Catch ex As Exception
    
                End Try
    
            Next
    
            For Each gevonden_folder As String In Tijdelijke_Bestanden
                Try
                    Directory.Delete(Tijdelijke_Bestanden, True)
    
                Catch
                End Try
            Next
    
        End Sub
    
        Private Sub Te_Bestand_Werker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles Te_Bestand_Werker.ProgressChanged
            Bibliotheek__Progressbar.Value = e.ProgressPercentage()
        End Sub
    
        Private Sub Te_Bestand_Werker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Te_Bestand_Werker.RunWorkerCompleted
            '
            '   Status laten zien wat er gedaan is.
            Me.Bibliotheek__Progressbar.Value = 100
            Me.Bibliotheek__Pbar_Textblock_01.Text = TEMP_Bestanden_Teller
            Me.Bibliotheek__Pbar_Textblock_02.Text = " Bestanden uit de tijdelijke folder verwijdert."
            Me.Bibliotheek__Button_01.IsEnabled = True
            '
        End Sub
    #End Region
    
    #Region " IE Cookies Achtergrond werker "
        Private Sub IE_Cookies_Werker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles IE_Cookies_Werker.DoWork
    
        End Sub
    
        Private Sub IE_Cookies_Werker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles IE_Cookies_Werker.ProgressChanged
    
        End Sub
    
        Private Sub IE_Cookies_Werker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles IE_Cookies_Werker.RunWorkerCompleted
    
        End Sub
    #End Region
    
    #Region " IE Bestanden Achtergrond werker "
        Private Sub IE_Bestand_Werker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles IE_Bestand_Werker.DoWork
    
        End Sub
    
        Private Sub IE_Bestand_Werker_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles IE_Bestand_Werker.ProgressChanged
    
        End Sub
    
        Private Sub IE_Bestand_Werker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles IE_Bestand_Werker.RunWorkerCompleted
    
        End Sub
    #End Region
    
    #End Region
    
    #Region " Button Click "
        '
        Private Sub Bibliotheek__Button_01_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            '
            '   Starten we het werk.
            Me.Te_Bestand_Werker.RunWorkerAsync()
            Me.Bibliotheek__Button_01.IsEnabled = False
            '
        End Sub
    
        Private Sub Bibliotheek__Button_02_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            Dim Internet_Bestanden As New System.IO.DirectoryInfo("C:\Users\" + Environment.UserName + "\AppData\Local\Microsoft\Windows\Temporary Internet Files")
            Try
                Directory.Delete("C:\Users\" + Environment.UserName + "\AppData\Local\Microsoft\Windows\Temporary Internet Files", True)
            Catch ex As Exception
                '   gewoon verder gaan met verwijderen van de andere bestanden.
            End Try
        End Sub
    
        Private Sub Bibliotheek__Button_03_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            Dim Internet_Cookies As New System.IO.DirectoryInfo("C:\Users\" + Environment.UserName + "\AppData\Roaming\Microsoft\Windows\Cookies")
            Try
                Directory.Delete("C:\Users\" + Environment.UserName + "AppData\Roaming\Microsoft\Windows\Cookies", True)
            Catch ex As Exception
                '   gewoon verder gaan met verwijderen van de andere bestanden.
            End Try
        End Sub
    
        Private Sub Bibliotheek__Button_04_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            '   Defragmenteer de harde schijf / schijven.
            System.Diagnostics.Process.Start("C:\Windows\System32\dfrgui.exe")
        End Sub
    
        Private Sub Bibliotheek__Button_05_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
            '   Ga naar de Bibliotheek online.
            System.Diagnostics.Process.Start("http://milarenstudio.wordpress.com/bibliotheek/")
        End Sub
        '
    #End Region
    
    #Region " Bibliotheek Afsluiten "
        '
        Private Sub Bibliotheek__Afsluiten(sender As System.Object, e As System.Windows.RoutedEventArgs)
            Me.Close()
        End Sub
        '
    #End Region
    
    End Class
    


    More knowledge, means more problems.

    Tuesday, July 17, 2012 8:05 PM

Answers

  • You may need to set

    worker.WorkerReportsProgress = true;

    and you'll also need to call

    worker.ReportProgress(<percent done>); from within the Do_Work(...) method.

    Warm regards,

    Matt

    • Marked as answer by Jie Bao Friday, July 27, 2012 10:24 AM
    Wednesday, July 18, 2012 7:40 AM

All replies

  • I'm guessing the problem is that you are attempting to modify the progress bar from the background worker which is a no no.  You need to invoke any changes to be applied to a control to the UI Thread otherwise it will fail.

    Michael Stacie

    Tuesday, July 17, 2012 8:12 PM
  • You may need to set

    worker.WorkerReportsProgress = true;

    and you'll also need to call

    worker.ReportProgress(<percent done>); from within the Do_Work(...) method.

    Warm regards,

    Matt

    • Marked as answer by Jie Bao Friday, July 27, 2012 10:24 AM
    Wednesday, July 18, 2012 7:40 AM