Question Cross-Thread call

Budius

Well-known member
Joined
Aug 6, 2010
Messages
137
Location
UK
Programming Experience
3-5
showing the whole code here would be too big and unimportant, so it's a question based on a pseudo-code.

The application on form load initialise and start 4 background workers
VB.NET:
Dim myInt as integer = 0

Private Sub ThreadsWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) _
     Handles BGThread_1.DoWork, _
          BGThread_2.DoWork, _
          BGThread_3.DoWork, _
          BGThread_4.DoWork

     dim x as integer
     dim y as integer = RandomInt

     While(True)
          x = myInt
          ' some math with x and y

          myInt = x*y
          Thread.Sleep(RandomBetween(500, 1500))

     End While

End Sub

Consider the code is not just dealing with integer but one thread gets a string, breaks it down to parts, converts them to integers or doubles, whilst other threads read those values and put them into labels or datagridviews.

Questions:
- will it at some random time give me an error because more than one thread is trying to access the variable?
- the code is compiled and installed in a client machine, what will be the behaviour of this machine whenever the error occurs? app freezes and have to shut from task manager? machine freezes and you have to pull the power plug? some .net framework pop-up error will come up?

thanks.
 
Last edited:
Based on my experience, random lockups requiring a Task Manager shutdown, with no 'apparent' error messages.

Resolve it by holding your integers in classes, and 'SyncLock'ing the classes to only allow one thread to access the class at any one time.
 
thanks mate,
cause of the size of the application and the lengthy annoying process of change request I am avoiding change it until I actually sure it might be it.
but you know how those issues are... never happen on your development platform.. only after you install it somewhere far far from you.

I've actually just started a new project with the code above and running on my machine and a virtual machine... but now luck to lock it up...
there are some synchronized events on the application that I might use ....

cheers.


====
edit:

JohnH,
thanks for the link... I'll have a read now.
 
Back
Top