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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB CriticalSection-like function

Status
Not open for further replies.

BeanDog

Programmer
Jul 15, 2000
60
US
I need to send information over a Winsock TCP/IP connection and wait for a response before sending any further information. Here is my generalized function:

Private Sub LockingSend(s As String)
Do While m_bLocked
DoEvents
Loop

m_bLocked = True

Winsock.SendData s

Do While m_bLocked
DoEvents
Loop
End Sub


An event is called when they send back information, which sets m_bLocked to false. This seems to work when I do something like this:

LockingSend "Data1"
LockingSend "Data2"
LockingSend "Data3"

That is, the first one waits for it to finish before returning. The problem comes when the user hits a button or something and fires of an event which calls LockingSend as well. For whatever reason, this isn't getting blocked by the first loop. Any idea why?

What I really want to do is have a critical section that lets VB handle other events while it's waiting for EnterCriticalSection to return. Any ideas?



~BenDilts( void );
 
Wonder if DoEvents is part of the problem - I think it basically says handle any other new requests before continuing on in the loop - example new request to send data.

What about sticking with a global flag and using a wait array. Something like:

Process1 - a request to send data
Process1 - checks busyFlag which is currently False
Process1 - sets busyFlag to True
Process1 - enters Critical Section (sends data)
Process2 - a request to send data
Process2 - checks busyFlag which is currently True
Process2 - assigned to waitArray
Process1 - recieves data
Process1 - Exits critical section
Process1 - sets busyFlag to False
This action causes an event to fire - check waitArray
Check waitArray finds Process2 is waiting index = 0
Check Array reactivates Process2
Process2 Checks busyFlag which is currently False
Process2 sets busyFlag to True
Process2 - enters Critical Section (sends data)


 
I agree with Keven. You REALLY don't want "DoEvents", as
that will "Do any event that is pending.". If you are still
getting undesired interruptions, you can disable all other
processes - this is needed when you're reading data from
a tape.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top