Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Threading VB.NET
Hello programmers. Sorry for my bad English
I would like to ask about thread. I created a program that uses thread. But I do not understand how to make the thread can sync with one another. Code like below:
Imports System.Threading
Imports System.Runtime.CompilerServices
 
Public Class EnvironmentTest
 
    Shared Sub Main()
        Dim objA As New Scrambler(0, 0)
        Dim objB As New Scrambler(0, 2)
        Dim objC As New Scrambler(0, 4)
 
        objA.ThreadWorker.Start()
        objB.ThreadWorker.Start()
        objC.ThreadWorker.Start()
    End Sub
End Class
 
Public Class Scrambler
    Public rnd As New Random
    Public _Number As Integer
    Public ThreadWorker As New Thread(AddressOf Random)
    Public _Left As Integer
    Public _Top As Integer
    Sub New(ByVal Left As Integer, ByVal Top As Integer)
        Me._Left = Left
        Me._Top = Top
        'ThreadWorker.IsBackground = True
    End Sub
    <MethodImpl(MethodImplOptions.Synchronized)> _
    Public Sub Random()
        While True
            SyncLock Me
                _Number = rnd.Next(0, 9)
                Console.SetCursorPosition(_Left, _Top)
                Console.Write(_Number)
            End SyncLock
        End While
    End Sub
End Class 
 
The problem on the Random method that is in the class Scrambler. Every time The Console.Write execute, it moved to the next column (Console should only write in one column because Console.SetCursorPosition always execute before Console.Write executed).
 
variable rnd.next _Number = (0, 9)
that means the maximum value for the variable _Number is 0 to 9 or one digit. But in the code above, precisely on Console.Write(_Number), _Number printed is 2 digits even more.
 

Is there someone who can provide solutions?
Posted 1-Oct-10 20:27pm
Edited 2-Oct-10 21:12pm
v7
Comments
Sandeep Mewara at 2-Oct-10 2:10am
   
Edited and formatted your question.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You do indeed have a race condition. This is because you are locking Me, so each object is locking itself, not some shared object. So:
 
1) Don't use MethodImplOptions.Synchronized - ever!
 
2) Don't use SyncLock Me - ever!
 
3) Add a shared object to Scrambler: Private Shared _Lock As New Object()
 
4) In the Random method, use: SyncLock _Lock
 

Note: I don't do VB, so you might have to fix the syntax.

Nick
  Permalink  
Comments
adaapanya at 3-Oct-10 2:19am
   
Thanks, you give me the solutions
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thanks Nicholas, you gave me the solution
 
Here is the right code for Class Scrambler :
Public Class Scrambler
    Public rnd As New Random
    Public _Number As Integer
    Public ThreadWorker As New Thread(AddressOf Random)
    Public _Left As Integer
    Public _Top As Integer
    Public Shared _Lock As New Object 'Add this code
    Sub New(ByVal Left As Integer, ByVal Top As Integer)
        Me._Left = Left
        Me._Top = Top
        'ThreadWorker.IsBackground = True
    End Sub
    '<MethodImpl(MethodImplOptions.Synchronized)> _
    Public Sub Random()
        While True
            SyncLock _Lock
                _Number = rnd.Next(0, 9)
                Console.SetCursorPosition(_Left, _Top)
                Console.Write(_Number)
            End SyncLock
        End While
    End Sub
End Class 
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 341
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 220
3 Praneet Nadkar 197
4 Shweta N Mishra 161
0 OriginalGriff 8,149
1 Sergey Alexandrovich Kryukov 7,287
2 DamithSL 5,614
3 Manas Bhardwaj 4,986
4 Maciej Los 4,910


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 3 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100