Results 1 to 9 of 9

Thread: Listbox + Background Worker (Cross thread problem)

Threaded View

  1. #1

    Thread Starter
    Addicted Member HunterTTP's Avatar
    Join Date
    Jul 2012
    Posts
    146

    Question Listbox + Background Worker (Cross thread problem)

    Alright so I will try and make this as simple a possible. For this example, there is listbox1, and button3.

    There are 10 items in listbox1 (A-T).

    The user selects 1 item and then clicks Button3.

    Button3 Starts the background worker.

    BackgroundWorker3 downloads a file using My.Computer.Network.DownloadFile ("google.com/" & ItemSelection)

    ItemSelection is "Dim(ed)" in the background worker -> Dim ItemSelection = ListBox1.SelectedItem

    So what should happen is, the user selects the Item, The user clicks "go", the background worker attempts to download from (if selection was "A") google.com/A.

    But what actually happens is, I get a "cross thread" error about how I cannot reference back to ItemSelection because it is not being used on the thread is was created on.

    Example Code-

    Code:
    Imports System.IO
    Imports Shell32
    
    Public Class Form1
    
        Dim Desktop As String = My.Computer.FileSystem.SpecialDirectories.Desktop
        Dim Username As String = Environment.UserName
        Private P As Integer
    ...

    Code:
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    
            BackgroundWorker3.RunWorkerAsync()
    
        End Sub
    ...

    Code:
     Private Sub BackgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker3.DoWork
    
                Dim ItemSelection = ListBox1.SelectedItem
    
                My.Computer.Network.DownloadFile("http://google.com/" & ItemSelection, Desktop)
    
        End Sub
    The Error




    Any help would be great!


    Hunter
    Last edited by HunterTTP; Feb 17th, 2013 at 09:04 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width