Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3916

Console App: is DoEvents inside a (loop inside a) thread a good practice?

$
0
0

…or should it be myThread.YieldToNext or myThread.Sleep(n)?

I know a potential answer is “Make your code Event-driven instead”, but that would massively complicate architecture and readability. I’d only consider it if my way has the potential to lead to serious trouble.

The loop in question is this, runs in a thread, currently employing YieldToNext:

while WorkerThread.BytesReceived < WorkerThread.SocketRef.RequestContentLength
  
  if WorkerThread.GetReceiveBufferChunks > 0 then
    
    stream.Write(WorkerThread.ReceiveBuffer(0))
    WorkerThread.BytesReceived = WorkerThread.BytesReceived + WorkerThread.ReceiveBuffer(0).Bytes
    WorkerThread.ReceiveBuffer.RemoveAt(0)
    lastTX = DateTime.Now
    
  end if
  
  // receive timeout check
  if DateTime.Now.SecondsFrom1970 - lastTX.SecondsFrom1970 > ipservercore.TimeoutOnReceive then // transfer has timed out
    stream.Close
    call infoplastique.DeleteFSitems(created)
    WorkerThread.SocketRef.RespondInError(408)  // Request Timeout 
    Return
  end if
  
  // no op
  WorkerThread.YieldToNext
  
Wend

A secondary question: Is it true that Xojo yields on Wend anyway? In this case, my YieldToNext is unnecessary. Would it be better in any way, to have a Sleep(something) instead?

Thanks!

5 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 3916

Trending Articles