So you need to know the number of instances you need beforehand.It must be set before the call to Server.Start.
Yes, I understand; this way I will have two threads, better than one only. But then I should use anyway the timers in this second thread, right?2) You can have a single background worker that creates any number of the game room classes. It is better than letting the main thread do the work as the main thread will be free for other tasks.
Yes.But then I should use anyway the timers in this second thread, right?
This is the question. I'm still trying to solve the sync problems and searching for an (the) "ideal" way .However I don't recommend you to do it until you find out that it is really required.
Can I have problems if I start many timers (500 or 1000, I hope ) in the main thread?
You can have 1000 timers if the interval is not too short.
I don't understand why you want to use so many timers. You just need a loop (or I don't understand the request):This is the question. I'm still trying to solve the sync problems and searching for an (the) "ideal" way .
The idea is:
1) the server sends to all the clients a "job" to do;
2) each client complete its job and send a "done" to the server;
3) when all "done" are received, the server executes its next job (not waiting, it meanwhile will handle other group of clients - game room).
But the server should set a timeout for the client's job, then it should use a timer for this and so there will be "many" active timers (with interval usually set to 20 s.).
All this is the reason for the question:
Determining how many clients can be managed seems to me an arduous task.
Dim AllDoneReceived As Boolean = True 'should be global and set to true the first time and when all done are received
Dim Start As Long = DateTime.Now
Do While True
Dim CurrentTime As Long = DateTime.Now
Dim Elapsed As Long = CurrentTime - Start
If Elapsed > 20000 Or AllDoneReceived Then
'SEND TO ALL CLIENTS
'...
Start = CurrentTime
End If
Loop
this must be run on a background thread.
Yes, you should create a thread for each game room, but you can do it with AddBackgroundWorker instead of using the Thread library.Thank you, @Informatix, that could be a better solution.
If I'm not mistaken, there is a thread library that also works with b4j.
In my case I should create a thread for each game room and dynamically, because the rooms are created and destroyed. I never used that library so I do not know if it's possible to create and destroy threads and "bind" them to the game rooms.
Will all this work be lighter for the server?
(in your code you should not declare long variables inside the loop )
Yes but I cannot create them dynamically, unfortunatelyBackground worker is a background thread.
It's not the DoWhile that is very important here, it's the loop contents. I should have put all of this in a Tick event of a single timer, I agree, but the interval of this timer should be set to something a lot smaller than 20 sec if you want to handle cases like this one, which shows the interest of this approach:A timer will be more efficient than a busy loop unless the wait period is very very short. It is true that you can use a single timer with a short interval.
Dim CurrentTime As Long = DateTime.Now
For i = 0 to MAX_CLIENTS - 1
Clients(i).Elapsed = CurrentTime - Clients(i).Start
If Clients(i).Elapsed > Clients(i).TimeOut Then
...
Clients(i).Start = CurrentTime
End If
Next
I don't have a timer for each player, of course, but one for each game room (not exactly, I used an additional class).It avoids the management of hundreds of timers, which would turn quickly into a nightmare and does not work very well in practice.