Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
VB
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
Updated 2-Oct-10 21:12pm
v7
Comments
Sandeep Mewara 2-Oct-10 2:10am    
Edited and formatted your question.

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:
VB
Private Shared _Lock As New Object()<br />
<br />
4) In the <code>Random
method, use: SyncLock _Lock


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


Nick
 
Share this answer
 
Comments
adaapanya 3-Oct-10 2:19am    
Thanks, you give me the solutions
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 
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900