Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,086 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,773 people online right now. Registration is fast and FREE... Join Now!




Backgroundwork Component

 
Reply to this topicStart new topic

Backgroundwork Component, Multithreading with backgroundwork

richard@thomson-intermedia.com
3 Jun, 2008 - 02:19 AM
Post #1

New D.I.C Head
*

Joined: 3 Jun, 2008
Posts: 1

I have been using the VB.net backgroundwork component on a couple programs that check files or delete files where I report the current work back to the UI though backgroundwork1_reportprogress. The worker class is setup like this
CODE

Public Class CurrentJob
    Public Class CurrentWorkState
        Public CurrentWork As String
        Public Dirwork As String
    End Class
Other code
EndClass


I then set the data for CurrentWork in the work class like this

CODE

Dim State As New CurrentWorkState
        State.CurrentWork = "Starting"
        State.Dirwork = "Starting"
        Worker.ReportProgress(0, State)


I then pickup the changes to currentwork in progressChanged and display the current data that I have passed normally the current file that is being copied or deleted. Code as follows
CODE

Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        Dim state As CurrentJob.CurrentWorkState = CType(e.UserState, CurrentJob.CurrentWorkState)
        Me.lblcurrprogress.Text = state.CurrentWork.ToString
        'Me.lbldirworktext.Text = state.Dirwork.ToString
    End Sub


As you can see I also pass some information to the UI via the DirWork and display the information in a different label. My problem is that when I add this line off code in after a short period off time (I would say the first time the progress changed tries to update the second label), an execption is thorwn, the exception is "System.Reflection.TargetOnvocationException" exception has been thrown by the target of an invocation. The innerexecption is "Object reference not set to an instance of an object" and this is the stack trace from the inner exception "at Synchierarchies.frmsynchierarchies.BackgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e) in E:\\Sync Hierarchies\\DeleteFiles\\frmsync1.vb:line 81\r\n at System.ComponentModel.BackgroundWorker.OnProgressChanged(ProgressChangedEventArgs e)\r\n at System.ComponentModel.BackgroundWorker.ProgressReporter(Object arg)".

Can any see where I have gone wrong or what is going wrong?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:51PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month