Results 1 to 2 of 2

Thread: Updating GUI with backgroundworker

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2008
    Posts
    74

    Updating GUI with backgroundworker

    I was hoping I can do that with timer, but its not working, like anything else I tried & all the codes I found I simply Dont understand ( Even TUTORIALS!!! why are they so complicated? ), so let ma ask for a simple example: how I make my label change?

    This is what I want to do:
    Code:
            For i = 0 To 1000
                System.Threading.Thread.Sleep(1000)
                Me.Label1.Text = i 
            Next
    And this is the code im working on:
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles Button1.Click
    
            Me.BackgroundWorker1.RunWorkerAsync() ' start worker
    
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, _
         ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
            For i = 0 To 1000
                System.Threading.Thread.Sleep(1000)
                Me.Label1.Text = i 
            Next
    
        End Sub
    
        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, _
         ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles _
          BackgroundWorker1.ProgressChanged
    
            ' not really shure how to use this...
    
        End Sub

    M.V.B. 2008 Express Edition

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Updating GUI with backgroundworker

    While it is technically not illegal to manipulate controls across threads, this is prone to some serious errors.

    Use these tutorials by jmc, I found them invaluable when getting acquainted with the BGW.

    Using the Background Worker Component


    Accessing Controls from Worker Threads

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