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

RE: Thread syncronization by AlexCBarberi

AlexCBarberi
Wed Jan 11 17:11:03 CST 2006

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

See this link
http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=1997&lngWId=10

--
Alex C. Barberi
Chief Executive Officer
VisionForce
http://www.visionforceweb.com



"Rob Smeets" wrote:

> 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

RE: Thread syncronization by Jon

Jon
Thu Jan 12 01:31:52 CST 2006

Alex C. Barberi <AlexCBarberi@discussions.microsoft.com> wrote:
> 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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too