Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 trying to understand threading

$
0
0
I am a beginner at threading right now and have been working off of the MSDN multi-threading post. I am attempting to use the example and count to 1000 add each number to a list box. while the program is counting to 1000 a picture will display. I am using a separate sub to count to 1000. I need to update the worker progress but am unsure of where to go from here. I have the code to the project below:
Code:

Imports System.ComponentModel

Public Class Form1
    Dim MasterReport As String
  Public Sub New()
        InitializeComponent()
        backgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
        Me.PictureBox1.Visible = False

    End Sub

    Private Sub startAsyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startAsyncButton.Click
        If BackgroundWorker1.IsBusy <> True Then
            ' Start the asynchronous operation.
            Me.BackgroundWorker1.RunWorkerAsync()
        End If
    End Sub

    Private Sub cancelAsyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancelAsyncButton.Click
        If BackgroundWorker1.WorkerSupportsCancellation = True Then
            ' Cancel the asynchronous operation.
            BackgroundWorker1.CancelAsync()
        End If
    End Sub

    ' This event handler is where the time-consuming work is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        System.Threading.Thread.Sleep(100)
        CountUp()
        worker.ReportProgress()
    End Sub
    Private Sub CountUp()
        For i = 0 To 1000
            Me.ListBox1.Items.Add(i)
        Next
    End Sub
    ' This event handler updates the progress.
    Private Sub backgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Me.PictureBox1.Visible = True
        Me.PictureBox1.BringToFront()
        Me.ListBox1.Items.Add(MasterReport)
        Me.ListBox1.SendToBack()
    End Sub

    ' This event handler deals with the results of the background operation.
    Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.PictureBox1.Visible = False
        Me.ListBox1.Items.Add("Finished")
    End Sub
End Class


Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>