Winsock and Re-entrancy 
Author Message
 Winsock and Re-entrancy

I'm using the winsock control on one of my VB6 forms.  From doing tests it
seems like winsock is re-entrant and the dataarrival event handler can be
fired even when it's already processing a message.

Some questions:
1)  How does winsock do this?  Does it use sendmessage so that it ignores
the queue?

2)  Is there any way for me to get around this so that I can process one
message without the fear of the dataarrival event handler being run again
while I'm doing the processing?

I've thought of creating of my own user created message and sending back
the message to my main application form using PostMessage
to solve the problem of re-entrancy.  Would that work?



Thu, 19 May 2005 11:51:33 GMT  
 Winsock and Re-entrancy

Quote:
> I'm using the winsock control on one of my VB6 forms.  From doing tests it
> seems like winsock is re-entrant and the dataarrival event handler can be
> fired even when it's already processing a message.

> Some questions:
> 1)  How does winsock do this?  Does it use sendmessage so that it ignores
> the queue?

> 2)  Is there any way for me to get around this so that I can process one
> message without the fear of the dataarrival event handler being run again
> while I'm doing the processing?

> I've thought of creating of my own user created message and sending back
> the message to my main application form using PostMessage
> to solve the problem of re-entrancy.  Would that work?

Check you're not using the VB equivalent of Application.ProcessMessages
(sorry, I write Delphi and I dont know what the VB version is) somewhere
else in your software. That tripped me up once.
if this isnt the problem, set a static boolean IsBusy var at the beginning
of the function to prevent entry until you've finished. Remember you'll have
to fire off another read if a message arrived.

Claire

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.422 / Virus Database: 237 - Release Date: 20/11/2002



Thu, 19 May 2005 16:20:09 GMT  
 Winsock and Re-entrancy

Quote:



>> I'm using the winsock control on one of my VB6 forms.  From doing
>> tests it seems like winsock is re-entrant and the dataarrival event
>> handler can be fired even when it's already processing a message.

>> Some questions:
>> 1)  How does winsock do this?  Does it use sendmessage so that it
>> ignores the queue?

>> 2)  Is there any way for me to get around this so that I can process
>> one message without the fear of the dataarrival event handler being
>> run again while I'm doing the processing?

>> I've thought of creating of my own user created message and sending
>> back the message to my main application form using PostMessage
>> to solve the problem of re-entrancy.  Would that work?

> Check you're not using the VB equivalent of
> Application.ProcessMessages (sorry, I write Delphi and I dont know
> what the VB version is) somewhere else in your software. That tripped
> me up once. if this isnt the problem, set a static boolean IsBusy var
> at the beginning of the function to prevent entry until you've
> finished. Remember you'll have to fire off another read if a message
> arrived.

Application.ProcessMessages sounds like it corresponds to DoEvents.

--
Max Bolingbroke MCP
http://www.bolingbroke.net/novanet/



Thu, 19 May 2005 16:34:05 GMT  
 Winsock and Re-entrancy

Quote:




>>> I'm using the winsock control on one of my VB6 forms.  From doing
>>> tests it seems like winsock is re-entrant and the dataarrival event
>>> handler can be fired even when it's already processing a message.

>>> Some questions:
>>> 1)  How does winsock do this?  Does it use sendmessage so that it
>>> ignores the queue?

>>> 2)  Is there any way for me to get around this so that I can process
>>> one message without the fear of the dataarrival event handler being
>>> run again while I'm doing the processing?

>>> I've thought of creating of my own user created message and sending
>>> back the message to my main application form using PostMessage
>>> to solve the problem of re-entrancy.  Would that work?

>> Check you're not using the VB equivalent of
>> Application.ProcessMessages (sorry, I write Delphi and I dont know
>> what the VB version is) somewhere else in your software. That tripped
>> me up once. if this isnt the problem, set a static boolean IsBusy var
>> at the beginning of the function to prevent entry until you've
>> finished. Remember you'll have to fire off another read if a message
>> arrived.

>Application.ProcessMessages sounds like it corresponds to DoEvents.

No I haven't used Do Events.  I know the application must be handling data
reentrantly because occasionally in the event handling I have a message
box pop up.  Normally when a message box pops up this stops the
application cold -- it doesn't do anything till the user clicks ok.

But in the compiled application it keeps on handling data being sent to
me.  More than one message box can even pop up.  Strangely enough, when I
run the application within the VB IDE, the application does stop handling
new data that's being sent to me.  So the problem only occurs when I run
the compiled application and not when I run it within the VB IDE.



Thu, 19 May 2005 17:04:07 GMT  
 Winsock and Re-entrancy

Quote:






>>>> I'm using the winsock control on one of my VB6 forms.  From doing
>>>> tests it seems like winsock is re-entrant and the dataarrival event
>>>> handler can be fired even when it's already processing a message.

>>>> Some questions:
>>>> 1)  How does winsock do this?  Does it use sendmessage so that it
>>>> ignores the queue?

>>>> 2)  Is there any way for me to get around this so that I can
>>>> process one message without the fear of the dataarrival event
>>>> handler being run again while I'm doing the processing?

>>>> I've thought of creating of my own user created message and sending
>>>> back the message to my main application form using PostMessage
>>>> to solve the problem of re-entrancy.  Would that work?

>>> Check you're not using the VB equivalent of
>>> Application.ProcessMessages (sorry, I write Delphi and I dont know
>>> what the VB version is) somewhere else in your software. That
>>> tripped me up once. if this isnt the problem, set a static boolean
>>> IsBusy var at the beginning of the function to prevent entry until
>>> you've finished. Remember you'll have to fire off another read if a
>>> message arrived.

>> Application.ProcessMessages sounds like it corresponds to DoEvents.

> No I haven't used Do Events.  I know the application must be handling
> data reentrantly because occasionally in the event handling I have a
> message box pop up.  Normally when a message box pops up this stops
> the application cold -- it doesn't do anything till the user clicks
> ok.

> But in the compiled application it keeps on handling data being sent
> to me.  More than one message box can even pop up.  Strangely enough,
> when I run the application within the VB IDE, the application does
> stop handling new data that's being sent to me.  So the problem only
> occurs when I run the compiled application and not when I run it
> within the VB IDE.

This re-entrancy is a feature of all OCXes (and ActiveX EXes AFAIK). You can
even see this behaviour with the Timer event of the Timer control. If you
really want to only process once you need something like IsBusy (as Claire
suggested)

Static IsBusy as Boolean

If IsBusy = False Then

    IsBusy = True

    'Process the data

    IsBusy = False

End If

HTH

--
Max Bolingbroke MCP
http://www.bolingbroke.net/novanet/



Thu, 19 May 2005 17:12:34 GMT  
 Winsock and Re-entrancy

<cut>

Quote:
> No I haven't used Do Events.  I know the application must be handling
> data reentrantly because occasionally in the event handling I have a
> message box pop up.

There's your problem.  Get it out of there or figure out a way to handle the
DataArrival events that come up while the messagebox is displayed.

Quote:
> Normally when a message box pops up this stops
> the application cold -- it doesn't do anything till the user clicks
> ok.

That's true in the VB IDE.  When compiled a messagebox runs apart and events
in the main application can still occur.

Quote:
> But in the compiled application it keeps on handling data being sent
> to me.  More than one message box can even pop up.  Strangely enough,
> when I run the application within the VB IDE, the application does
> stop handling new data that's being sent to me.  So the problem only
> occurs when I run the compiled application and not when I run it
> within the VB IDE.

Exactly.


Thu, 19 May 2005 23:50:16 GMT  
 Winsock and Re-entrancy
The socket will continue to receive data, even though you haven't finished
processing the previous message. If you set a flag, to ignore the second
message until you are finished with the current one, then you may loose
data. Your best bet is to eliminate your processing in the DataArrived
event, just like you would do in a timer event.

Typically, you would copy the data to some kind of buffer (in memory) in the
DataArrived event, and then process the data externally to the event. Make
sure your buffer is large enough to handle several messages. Beyond that,
without knowing the 'type' of application (i.e. frequency of messages, size,
etc.) that's the best recomendation.

I like a circular Shared Memory region as my buffer, and process the
messages on a timer (depending on the message type, of course - this is
suitable for large messages - since you will get a lot of small packets of
data very quickly).

Stephen J Whiteley


Quote:


> <cut>
> > No I haven't used Do Events.  I know the application must be handling
> > data reentrantly because occasionally in the event handling I have a
> > message box pop up.

> There's your problem.  Get it out of there or figure out a way to handle
the
> DataArrival events that come up while the messagebox is displayed.

> > Normally when a message box pops up this stops
> > the application cold -- it doesn't do anything till the user clicks
> > ok.

> That's true in the VB IDE.  When compiled a messagebox runs apart and
events
> in the main application can still occur.

> > But in the compiled application it keeps on handling data being sent
> > to me.  More than one message box can even pop up.  Strangely enough,
> > when I run the application within the VB IDE, the application does
> > stop handling new data that's being sent to me.  So the problem only
> > occurs when I run the compiled application and not when I run it
> > within the VB IDE.

> Exactly.



Fri, 20 May 2005 21:55:36 GMT  
 Winsock and Re-entrancy


Quote:
> The socket will continue to receive data, even though you haven't finished
> processing the previous message. If you set a flag, to ignore the second
> message until you are finished with the current one, then you may loose
> data. Your best bet is to eliminate your processing in the DataArrived
> event, just like you would do in a timer event.

Can you explain why you believe he will lose data if he doesnt process it
immediately? I disagree.
Winsock's buffers will fill and it wont accept more incoming until you empty
some of it. Integrity of data is major requisite of streaming TCP. There
will be a bottleneck at the other end of the connection as the sending
socket becomes blocked. Sender winsock should be returning error
WSAWouldblock to it's own calling function.
Refusing entry by using a flag will cause the notification to be lost, the
data will still be there until you read it. There's nothing stopping you
querying socket status before you exit function

--

Claire

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.427 / Virus Database: 240 - Release Date: 06/12/2002



Mon, 30 May 2005 05:20:10 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Error 3525: Illegal reentrancy during query execution - VB3/Access1.1

2. A tricky Active-X reentrancy issue and Rube Goldberg solution

3. Floating point processor and re-entrancy?

4. Floating point processor and re-entrancy

5. MSComm question and DoEvents (re-entrancy)

6. Reentrancy bug in Inet Transfer Ctrl?

7. Reentrancy

8. Re-entrancy???

9. ODBC causing Winsock "TCP Poll reentrancy detected" error

10. winsock problem sending large strings (winsock.senddata)

11. Multiple Winsock-Connections to a server AND Winsock-Connection to a server minimized in systray

12. Winsock OCX vs Winsock API?

 

 
Powered by phpBB® Forum Software