Question System.Threading.Timer freezing computer


New member
Mar 27, 2017
Programming Experience
I am working on creating a game, and I have a pathfinding function that takes about 100 ms. I have 5 enemies, each with this function in the contstructor:

[COLOR=#303336]newPath [/COLOR][COLOR=#303336]= [/COLOR][COLOR=#101094]new [/COLOR][COLOR=#2B91AF]System[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Threading[/COLOR][COLOR=#303336].[/COLOR][COLOR=#2B91AF]Timer[/COLOR][COLOR=#303336](([/COLOR][COLOR=#303336]e[/COLOR][COLOR=#303336])[/COLOR][COLOR=#303336]=>[/COLOR][COLOR=#303336]{[/COLOR][COLOR=#303336]
  getNewPath[/COLOR][COLOR=#303336]();[/COLOR][COLOR=#858C93]//The function that takes ~100 ms[/COLOR][COLOR=#303336]

Now, I am using System.Threading.Timer at an earlier point in the program (to run once every 50 ms just for a step function, to update positions and such). That one works fine, but if I run this function (don't forget I have 5 enemies, so it's running 5 times every 5 seconds), my whole entire computer just freezes. Now I don't have a crappy computer (it's not the best, but it's plenty good for what i'm using it for), so I don't know what the issue is. Even if all timers run one after the other (which they shouldn't do, they should run at the same time), the most it should take is 500ms (or half a second), yet it completely kills my computer, to the point where my mouse doesn't move, I can't Ctrl-Alt-Del, and I have to just hold the power button until it turns off.
I tested putting a simple print function in place of the getNewPath(), and it worked flawlessly and as expected, so I don't really know what the issue is.
My questions are:

  1. What is causing my computer to lock up to the point of having to hold the power button.
  2. Is there something I can use other than System.Threading.Timer that will give me the desired result without completely killing my computer? (Being able to run this function up to ~20 times at once, since it's an MMO and there could potentially be hundreds of enemies that it needs to do pathfinding updates on).
Top Bottom