Backgroundworker doesn't move files to correct directories vb.net

I am working on a application that extracts and moves files around to specific directories... At the same time deletes the files it used to extract, and etc etc etc. Just like a basic installer.

Here is what I'm having problems with:

   Dim DirectoryToInstall As String = Browse.TextBox.Text

Private Sub InstallMods()

    If HPV2.Checked = True Then
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\gui\Scaleform\FragCorrelation.swf", (DirectoryToInstall) & "\res_mods\0.9.4\gui\scaleform\FragCorrelation.swf")
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\scripts\client\mods\TeamHPPools.pyc", (DirectoryToInstall) & "\res_mods\0.9.4\scripts\client\mods\TeamHPPools.pyc")
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\scripts\client\gui\scaleform\daapi\view\lobby\settings\settingswindow.pyc", (DirectoryToInstall) & "\res_mods\0.9.4\scripts\client\gui\scaleform\daapi\view\lobby\settings\settingswindow.pyc")
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\ModSettings\ModSettings.cfg", (DirectoryToInstall) & "\res_mods\ModSettings\ModSettings.cfg")
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\ModSettings\MultilinedTankCarousel.cfg", (DirectoryToInstall) & "\res_mods\ModSettings\MultilinedTankCarousel.cfg")
                My.Computer.FileSystem.MoveFile(appData & "\Svatekl3\Svatekl2\Team HP Idicator\V3\ModSettings\Team HP Pools and Healthbar.cfg", (DirectoryToInstall) & "\res_mods\ModSettings\Team HP Pools and Healthbar.cfg")
            End If

End Sub

(This is just a sample code... There is a ton more to it :P ) There are around 200 files that it has to move, depending if the person checks a check mark to move those files or not.

This is how my backgroundworker looks like:

Private Sub BackgroundWorker_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
    Dim DirectoryToInstall As String = Label4.Text

    DeleteTemp()

    CleanMods()

    MoveZip()

    MyExtract()


    CreateDirs()

    InstallMods()

    DeleteTemp()

    Finish.StartPosition = FormStartPosition.CenterScreen
    Finish.Show()
    Me.Hide()
End Sub

It's pretty self-explanatory... The DeleteTemp() delete's files that were created by the program that are not needed.

The CleanMods() deletes files in a game directory selected by the user, to make sure all the mods he installs using the installer work.

The MoveZip() moves a zip file from resources to appData.

MyExtract() Extracts those files

CreateDirs() Creates folders for the files

InstallMods() moves the files into the correct directories

And I do DeleteTemp() again...

I explained this all so you knew what the program is supposed to do...

Now back to my problem... If I use the UI Thread, everything works perfectly! It deletes all the files, installs all the files, extracts everything, and etc.

The only draw-back is the UI get's frozen.

Now I thought of using a secondary thread and nothing really worked out :(

So I decided to give backgroundworker a shot... I put all my code inside the Backgroundworker and ran the program.

Every time the program gets to the part to install the files, it tells me that "This files already exists"..

But at the same time I was watching my folder, and it looks like the backgroundworker has the wrong directory... Because it wasn't installing the files into the directory (nor did it make any changes to that directory at all)

What I mean is... Let's say that Dim DirectoryToInstall As String = TextBox1.Text

The TextBoxt1.Text = Whatever the user selects in the file browser.

For instance I select the folder "Desktop"...

So the program INSTEAD of installing the files onto desktop installs them to some other folder.

Why does this happen?

If I use the UI thread.. There are no issues.

I heard that backgroundworker, since it uses a different thread, cannot receive any information from the UI thread.

So I somehow have to send that information to my backgroundworker myself, but how?

Update:

Looks like backgroundworker doesn't want to run this code:

Private Sub CleanMods()


        Dim appData As String = GetFolderPath(SpecialFolder.ApplicationData)
        If CleanOutFolders.RadioButton1.Checked = True Then

            Try
                Dim path As String = (DirectoryToInstall) & "\res_mods"
                My.Computer.FileSystem.DeleteDirectory(path, FileIO.RecycleOption.DeletePermanently, FileIO.UICancelOption.DoNothing)
            Catch
                MessageBox.Show("res_mods not found!")
            End Try

            Try
                FileSystem.Kill((DirectoryToInstall) & "\res\audio\xvm.fsb")
                FileSystem.Kill((DirectoryToInstall) & "\res\audio\xvm.fev")
                FileSystem.Kill((DirectoryToInstall) & "\res\audio\voice.fsb")
                If File.Exists((DirectoryToInstall) & "\res\audio\voice.fev") Then
                    FileSystem.Kill((DirectoryToInstall) & "\res\audio\voice.fev")
                End If
            Catch
            End Try
        End If
    End Sub
Jon Skeet
people
quotationmark

Currently, you're accessing the UI in the background worker, which you shouldn't - it's not running on the UI thread.

One overload of BackgroundWorker.RunWorkerAsync has an Object parameter, and this is the value that will available as DoWorkEventArgs.Argument - so that's the best way of communicating the "input" to the task. If you're just passing a single value (e.g. a string) you can do that directly; otherwise, create a class to wrap all the information in a single object.

Additionally, your last three lines access the UI too, so they should be in the handler for the BackgroundWorker.RunWorkerCompleted event, which will be executed on the UI thread.

people

See more on this question at Stackoverflow