NetworkServerEvent and WaitWindowEvent

Just starting out? Need help? Post your questions and find answers here.
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

NetworkServerEvent and WaitWindowEvent

Post 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.
User avatar
Shardik
Addict
Addict
Posts: 2067
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Post 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
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post 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.
Tranquil
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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!"™
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post 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>
I love Purebasic.
freak
PureBasic Team
PureBasic Team
Posts: 5948
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.)
quidquid Latine dictum sit altum videtur
Post Reply