Page 1 of 1

NetworkServerEvent and WaitWindowEvent

Posted: Fri Feb 27, 2009 6:04 pm
by Xombie
Good morning! What is the best practice for checking window events and server events at the same time in the same application? I've messed with one or the other but never trying to work with both at the same time.

[Edit] I should add that in a previous application I created a thread with a repeat/forever loop and a 5 ms delay that continuously checked for network events. If that's the best way then I'll go with that again.

Posted: Tue Mar 03, 2009 4:01 pm
by Shardik
I would create a separate thread which detects network events and sends a

Code: Select all

PostMessage_(WindowID(0), #WM_USER+1, 0, 0)
to simulate a custom window event which can be handled in the window event loop. For an example take a look into an old posting from nco2k in the German forum which sends an event after counting to 100 in the separate thread (has to be adapted to PB 4): http://www.purebasic.fr/german/viewtopi ... 71&start=4

Posted: Tue Mar 03, 2009 7:50 pm
by Tranquil
I prefere the Window-Event based method. I'm using WSAsyncSelect_() to bring network event messages to the main loop.

Otherwise you can also use the pooling-method and just add a delay(0) to your mainloop if no event is in queue.

Posted: Tue Mar 03, 2009 7:55 pm
by Trond
Normally you want your a separate thread for networking to make sure your user interface is always responsive. But if not, "a delay() is the way!"™

Posted: Wed Mar 04, 2009 4:08 am
by Heathen
I normally do something like this:

Code: Select all

Repeat
  netevent = NetworkServerEvent()
  winevent = WindowEvent()
  If winevent = 0 And netevent = 0
    Delay(1)
    Continue
  EndIf
 ;handle events

Until <whatever>

Posted: Wed Mar 04, 2009 6:08 am
by freak
Better use WaitWindowEvent(1) instead of WindowEvent() + Delay(1).

The WaitWindowEvent(1) will wake up if there is a window event while waiting. The Delay(1) will keep waiting until the time elapses (which is more than 1ms usually btw.)