Thread syncronization

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

i'm trying to grasp the concept of syncronizing threads, but without success

Here is the situation:

I'm using an object which connects to a server. After connection i want to
send informational requests.
If i send a request and the object is not connected yet i tell the Component
to connect(sub RequestInfo).
But before sending the request i have to wait until the connection succeded.
This is told to me by a Event (Sub Open_Result)
There are 2 options 1 is by using doEvents() in a loop, but that is using up
resources, the second is syncronizing Threads.
Could anyone help me?

Thanks in advance!

Rob Smeets

See below for simplified sample

Class GetInfo
Private withevents objInformationService as New InformationService
Private Connected as boolean = False

private Sub Connect()
objInformationService.Connect("UserName", "Password")
end sub

Private Sub RequestInfo(SelectStatement as string)
If Me.connected = False then
Me.Connect()
End if


objInformationService.RequestInfo(SelectStatement)
End sub

Private Sub Open_Result(Result as integer, ErrorMessage as string) handles
objInformationService.Openresult
If result = 1 then
Connected = True
Else
msgbox ErrorMessage
end if
End Sub
End Class
 
Alex C. Barberi said:
Okay, go ahead and import System.Threading

Now, there are two ways to synchronize threads, one ways is to use
Monitor.Enter, Monitor.Wait, and Monitor.Exit; the other way is to use the
SyncLock blocks. Example:
SyncLock (Me)
' code that requires synchronization goes here
End SyncLock

Note that it's a bad idea (IMO) to lock on "Me" (VB.NET) or "this"
(C#). See
http://www.pobox.com/~skeet/csharp/threads/lockchoice.shtml for my
reasoning.
 
Back
Top