Android Question [SOLVED] Sleep inside an Event

sirjo66

Well-Known Member
Licensed User
Hello to all !!

How can I wait a second inside an Event ??

Ok ok, I explain better.

I have an HttpServer and with HandleRequest event I accept a GET request and I need to answer, but first I need to wait 1 second.
Here is the source that works correctly:
B4X:
Sub Server_HandleRequest(Request As ServletRequest, Response As ServletResponse)
   ' Wait one second
   Dim Adesso As Long = DateTime.Now + DateTime.TicksPerSecond ' aspetta un secondo
   Do Until DateTime.Now >= Adesso
      DoEvents
   Loop
   ' send the response
   Response.SetContentType("text/html")
   Response.SendString("Perfect, all right !")
End Sub

But, as all the world knows, "DoEvents" is deprecated, but if I change to
B4X:
Sub Server_HandleRequest(Request As ServletRequest, Response As ServletResponse)
   ' Wait one second
   Sleep(1000)
   ' send the response
   Response.SetContentType("text/html")
   Response.SendString("Perfect, all right !")
End Sub
it don't works, because when the code execute Sleep line, it exit from the event, and Response.SendString don't work

How to solve it ??

Thanks
 
Last edited:

DonManfred

Expert
Licensed User
 
Upvote 0

Star-Dust

Expert
Licensed User
But StartMessageLoop isn't it just for B4J?
 
Upvote 0

Star-Dust

Expert
Licensed User
Hello to all !!

How can I wait a second inside an Event ??

Ok ok, I explain better.

I have an HttpServer and with HandleRequest event I accept a GET request and I need to answer, but first I need to wait 1 second.
Here is the source that works correctly:
B4X:
Sub Server_HandleRequest(Request As ServletRequest, Response As ServletResponse)
   ' Wait one second
   Dim Adesso As Long = DateTime.Now + DateTime.TicksPerSecond ' aspetta un secondo
   Do Until DateTime.Now >= Adesso
      DoEvents
   Loop
   ' send the response
   Response.SetContentType("text/html")
   Response.SendString("Perfect, all right !")
End Sub

But, as all the world knows, "DoEvents" is deprecated, but if I change to
B4X:
Sub Server_HandleRequest(Request As ServletRequest, Response As ServletResponse)
   ' Wait one second
   Sleep(1000)
   ' send the response
   Response.SetContentType("text/html")
   Response.SendString("Perfect, all right !")
End Sub
it don't works, because when the code execute Sleep line, it exit from the event, and Response.SendString don't work

How to solve it ??

Thanks
It also happened to me in the management of the library for Zoom Cloud Meeting.
Many things had to be managed by the service (because the main activity was paused and queued the events)
But some events raised in the service required a Sleep that didn't work ......

I can't tell you how I solved it, too unprofessional 😂 😂 😂 😂 😂

PS. I obviously speak of b4a and not B4j as @DonManfred rightly points out
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
OP is using the quite old HttpServer library, not B4J jServer

The way that library is built (several years before resumable subs were available) is that the response is committed after the code returns from this sub. Rule number #1 of resumable subs is that from the calling method point of view, the code returns when there is a call to Sleep or Wait For.
Bottom line is that you cannot use Wait For or Sleep here.
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
OP is using the quite old HttpServer library, not B4J jServer

The way that library is built (several years before resumable subs were available) is that the response is committed after the code returns from this sub. Rule number #1 of resumable subs is that from the calling method point of view, the code returns when there is a call to Sleep or Wait For.
Bottom line is that you cannot use Wait For or Sleep here.
Many thanks, I continue to use DoEvents and a #IgnoreWarnings: 33
In the future please don't delete DoEvents command from B4A 😁
 
Upvote 0
Top