Hi guys
I seem to have thread safety issues in my code, whereby local variables are being overwritten within a backgroundworke r 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.Componen tModel.Backgrou ndWorker
AddHandler runme.DoWork, AddressOf run_it
runme.RunWorker Async(x)
loop
end sub
Private Sub run_it(ByVal sender As System.Object, ByVal e As System.Componen tModel.DoWorkEv entArgs)
Dim worker As System.Componen tModel.Backgrou ndWorker = CType(sender, System.Componen tModel.Backgrou ndWorker)
Dim whatever=e.Argu ment
' execute some code that takes varied time
SyncLock messagesLock
console.writeli ne(whatever & " result was such and such...")
End SyncLock
end sub
End Module
Thanks a million for any advice you can give
Cheers
Mark
I seem to have thread safety issues in my code, whereby local variables are being overwritten within a backgroundworke r 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.Componen tModel.Backgrou ndWorker
AddHandler runme.DoWork, AddressOf run_it
runme.RunWorker Async(x)
loop
end sub
Private Sub run_it(ByVal sender As System.Object, ByVal e As System.Componen tModel.DoWorkEv entArgs)
Dim worker As System.Componen tModel.Backgrou ndWorker = CType(sender, System.Componen tModel.Backgrou ndWorker)
Dim whatever=e.Argu ment
' execute some code that takes varied time
SyncLock messagesLock
console.writeli ne(whatever & " result was such and such...")
End SyncLock
end sub
End Module
Thanks a million for any advice you can give
Cheers
Mark