Connecting Tech Pros Worldwide Help | Site Map

vb .net background worker thread safety

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 13 '09
Hi guys

I seem to have thread safety issues in my code, whereby local variables are being overwritten within a backgroundworker sub routine. In the example below, sometimes the output will repeat the same whatever variable. I assume its because it is being overwritten?

Module Module1

Private messagesLock As New Object

sub main()

do while (x<10)

Dim runme As New System.ComponentModel.BackgroundWorker
AddHandler runme.DoWork, AddressOf run_it
runme.RunWorkerAsync(x)

loop

end sub

Private Sub run_it(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

Dim worker As System.ComponentModel.BackgroundWorker = CType(sender, System.ComponentModel.BackgroundWorker)

Dim whatever=e.Argument

' execute some code that takes varied time

SyncLock messagesLock
console.writeline(whatever & " result was such and such...")
End SyncLock

end sub

End Module

Thanks a million for any advice you can give

Cheers
Mark
Reply


Similar Visual Basic .NET bytes