Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TcpClient EndRead event not setting class variable even with SyncLock

Status
Not open for further replies.

cjelec

Programmer
Jan 5, 2007
491
GB
Hi,

I'm having some trouble with Raising an event in a EndRead callback which is not setting a class variable even though I'm using SyncLock?

TCP class:
Code:
Private Sub StartReader()
   _stream.BeginRead(_read_buffer, 0, _read_buffer.Length, New AsyncCallback(AddressOf ReturnReader), _parent)
End Sub

Private Sub ReturnReader(res As IAsyncResult)

   _read_buffer_length = _stream.EndRead(res)

   'The code to get the data from the buffer and store it into a larger buffer works fine
   '...

   RaiseEvent OnDataReceivedEvent(me)

End Sub

Connection Handler Class (server):
Code:
Private WithEvents _tcp As TCP
Private _user As User 'Class containing user details
Private _loggedin As Boolean
Private _lock As Object = New Object()

Public Sub New()

   _user = New User()

End Sub

Private Sub OnDataReceived(Sender As Object) Handles _tcp.OnDataReceivedEvent
   'Gets here fine, except _user is always the same as when the class was created
   
   Dim Username As String
   Dim Command As String
   
   Command = _tcp.ReadLine()
   'Simplified for this example

   If GetCommand(Command) = "username" Then
      Username = GetValue(Command)

      'Two ways I have tried this:

      'First:
      SyncLock _user
          _user = New User(Username)
      End SyncLock

      'Second way without setting the _user in the Sub New:
      SyncLock _lock
          _user = New User(Username)
      End SyncLock

   Else GetCommand(Command) = "password" Then

      '_user is still the blank one or is nothing if I don't set it in the Sub New
      Dim logged As Boolean

      SyncLock _user
          logged = _user.CheckPassword(GetValue(Command))
      End SyncLock

      If logged = True Then
         SyncLock _lock
            _loggedin = True
         End SyncLock
      End If

   End If
End Sub

I have also tried the following and getting the same results:
- SyncLock me
- Inheriting the Tcp class and Overriding a Sub that is called in the ReturnReader Sub
- Using a Function in the User class that sets the user, so not creating a new instance of the User class
- Googling for over 6 hours

Please can someone help me?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top