I'm programming a Game which needs a loop that
works off Events that are placed by different Objects.
I already thought of some approaches and tried some of them,
but I'm still unsure which I should take.
(A little introduction)
Every Building in the game produces events which are stored in an array and
if the time has passed (given by the event) the event should be executed.
Solutions:
1. Every Building has an thread which loops looking for new events to be executed.
At first I wanted to go with this version, but I was afraid that it could too much,
so I made a test where 1000 (timer)threads where made with almost nothing in the thread and
the processor load went up pretty high which so I tried to think of another solution.
2. One thread for every building
Going to the other extreme, with just one thread for every building,
I have the problem that I cannot set different intervals to every buildingkind and some other issues.
3. One thread per buildingkind
So now I think I'm going with this method which should be a good way inbetween.
The real problem that I'm having is that I can create those threadloops in three different ways and
I have no idea which suites my needs best.
1. Create an timerthread (Threading.Timer)
This has the problem that the thread could have nothing to do (no stored events) wasting cpu power.
2. Create an Thread with an endless loop wherein the thread sleeps after going through the stored events
3. Instead of putting the thread to Sleep I thought of ending the thread'S time slice so
that other threads are processed.
This method is the one I'm most interested because it (correct me if I'm wrong)
is able to ajust to different cpu speed lowering the load on the CPU.
I thought with (Threading.Thread.SpinWait) I would be able to do that but
the CPU load went up to 100% even worse than with the Timers and
if I set the iterations very high the movement of the form stutters.
What is an interation in SpinWait exactly?
Is there any better way to lower the load on the CPU?
works off Events that are placed by different Objects.
I already thought of some approaches and tried some of them,
but I'm still unsure which I should take.
(A little introduction)
Every Building in the game produces events which are stored in an array and
if the time has passed (given by the event) the event should be executed.
Solutions:
1. Every Building has an thread which loops looking for new events to be executed.
At first I wanted to go with this version, but I was afraid that it could too much,
so I made a test where 1000 (timer)threads where made with almost nothing in the thread and
the processor load went up pretty high which so I tried to think of another solution.
2. One thread for every building
Going to the other extreme, with just one thread for every building,
I have the problem that I cannot set different intervals to every buildingkind and some other issues.
3. One thread per buildingkind
So now I think I'm going with this method which should be a good way inbetween.
The real problem that I'm having is that I can create those threadloops in three different ways and
I have no idea which suites my needs best.
1. Create an timerthread (Threading.Timer)
This has the problem that the thread could have nothing to do (no stored events) wasting cpu power.
2. Create an Thread with an endless loop wherein the thread sleeps after going through the stored events
3. Instead of putting the thread to Sleep I thought of ending the thread'S time slice so
that other threads are processed.
This method is the one I'm most interested because it (correct me if I'm wrong)
is able to ajust to different cpu speed lowering the load on the CPU.
I thought with (Threading.Thread.SpinWait) I would be able to do that but
the CPU load went up to 100% even worse than with the Timers and
if I set the iterations very high the movement of the form stutters.
What is an interation in SpinWait exactly?
Visual Basic:
Dim EventThread As New Threading.Thread(AddressOf EventHandler)
...
Private Sub EventHandler(ByVal State As Object)
Dim RegBuilding As Short
Dim RegBuildings As Short
Dim BuildingEvent As Short
Dim BuildingEvents As Short
Do
BuildingEvents = BuildingsDB.Count - 1
For RegBuilding = 0 To RegBuildings 'Loop though Registered Buildings
With PlayerDB(BuildingsDB.Item(RegBuilding).Player).BuildingDB(BuildingsDB.Item(RegBuilding).BuildingID)
'Point to the actual BuildingObject
BuildingEvent = .Events.Count
For BuildingEvent = 0 To BuildingEvents 'Work Off Events of the CurrentBuilding
.Events(BuildingEvent).Check() 'Check if it is time to execute Event
Next
End With
Next
Threading.Thread.Sleep(100)
Loop
End Sub
Is there any better way to lower the load on the CPU?