RTC Forums
November 23, 2024, 08:44:35 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Using WaitForSingleObject in DataReceived event  (Read 6866 times)
Ecole7
RTC Expired
*
Posts: 23


« on: May 16, 2017, 08:47:01 AM »

Hello,

I'd like to know if using the WaitForSingleObject() function in an RtcDataProvider DataReceived event can cause problems. Note that I used the mode "multithreaded".

R: = WaitForSingleObject (vbpid, 100);

This function is used to verify an external process written in Visual Basic. It allows me to check if this application is running.

Thank you in advance for your answer.

Vincent
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: May 16, 2017, 09:07:39 AM »

Since the WaitForSingleObject API is blocking and does NOT process Windows messages while waiting, provided your Server is Multi-Threaded and there are enough threads available in the RTC Thread pool for other things that should continue working while that thread is waiting, your main concern should be the total time you spend in that event, because (A) you are blocking a thread and (B) your connection could get lost or dropped while waiting, in which case the Server would be unable to send a response back to the Client. Other than that, I don't see any problems with using the WaitForSingleObject API from inside RTC events to wait for external signals.

Best Regards,
Danijel Tkalcec
Logged
Ecole7
RTC Expired
*
Posts: 23


« Reply #2 on: May 16, 2017, 05:51:38 PM »

Hello,

Thank you for the clarification.

Here are the changes I made to my code:
 
Function WaitForStatus: Boolean;
var
  tm: Integer;
begin
  Tm: = 100; // 1sec timeout
  With AppEcole [_ixa] do begin
   While (Tm> 0) do begin
    If State = VBEV_READY then begin
     Result: = True;
     Exit;
    end;
    Application.ProcessMessages;
    Sleep (10);
    Dec (tm);
   end;
  end;
  Result: = False;
end;

...
// check if the VB application has closed
R: = WaitForSingleObject (vbpid, 0); // no timeout!
If R <> WAIT_TIMEOUT then begin
  ...
End else if WaitForStatus then begin // read the status of the application
  ...
end;
...

The State variable is linked to the VB application (OLE automation)

For now, the tests are OK.
Is this code more compatible with the operation of the RTC components?

Thank you in advance for your answer.

Vincent
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #3 on: May 16, 2017, 06:32:14 PM »

It looks like there was a missunderstanding. I've said that using WaitForSingleObject is OK, because it is a blocking call which does NOT process Windows Messages. But ... the Application.ProcessMessages or any other API or method which DOES PROCESS Windows Messages should NEVER be called from any event triggered by RTC components.

There is a Quick-Start Topic about this.

Best Regards,
Danijel Tkalcec
Logged
Ecole7
RTC Expired
*
Posts: 23


« Reply #4 on: May 16, 2017, 07:22:16 PM »

OK,
But In my case I just had to monitor a variable for a given time. Is using RtcWaitFor possible?
The goal is to respond to an HTTP request with a certain delay that depends on an external application.
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #5 on: May 16, 2017, 08:39:16 PM »

Waiting inside RTC events is OK. Responding to Requests with a Delay is also OK. You will (obviously) be blocking a thread while waiting and might also end up with a closed connection if you wait too long, but that's something you should always take into account.

Processing Windows Messages while waiting inside RTC events is NOT OK. Because the RtcWaitFor function is designed to process Windows Messages and to execute events "synchronized with the Main Thread" by using the Sync method, it may ONLY be called directly from the Main Thread and NEVER from inside any events triggered by RTC components or code executed as a result of an event triggered by a RTC component.

Logged
Ecole7
RTC Expired
*
Posts: 23


« Reply #6 on: May 17, 2017, 06:03:44 AM »

Hello,

Thank you for your patience. I think I understood ...

1. Do not use Application.ProcessMessages in RTC components (threads) events.

2. It is possible to pause the event (thread) by the sleep () function,
but avoid having too many threads paused (check the value of RtcTotalThreadsBusy and react accordingly).

3. Use WebSockets (asychronous query) instead of HTTP (POST or GET).
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #7 on: May 17, 2017, 06:52:25 AM »

Whether you are going to use WebSockets or the usual request/response scheme over HTTP/S depends on your personal preferences and Application requirements. Both have positive and negative sides. But your other 2 points are correct.

Best Regards,
Danijel Tkalcec
Logged
Ecole7
RTC Expired
*
Posts: 23


« Reply #8 on: May 17, 2017, 07:52:09 AM »

Thank you for your reply,
For my application that worked in AJAX, the WEBSocket is useful because it allows to realize asynchronous requests, thus to avoid unnecessarily blocking threads to wait for a response.
Greetings
Vincent
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.026 seconds with 16 queries.