# How to create a thread in XNA for pathfinding?

I am trying to create a separate thread for my enemy's A* pathfinder which will give me a list of points to get to the player. I have placed the thread in the update method of my enemy. However this seems to cause jittering in the game every-time the thread is called.

I have tried calling just the method and this works fine. Is there any way I can sort this out so that I can have the pathfinder on its own thread? Do I need to remove the thread start from the update and start it in the constructor? Is there any way this can work?

Here is the code at the moment:

bool running = false;

public void update()
{
if (running == false && threadstarted == false)
{

}
}

{

object[] Startandendarray = (object[])Startandend;
Point startpoint = (Point)Startandendarray[0];
Point endpoint = (Point)Startandendarray[1];

bool runnable = true;
// Path find from 255, 255 to 0,0 on the map
foreach(Tile tile in Map)
{
if(tile.Color == Color.Red)
{
if (tile.Position.Contains(endpoint))
{
runnable = false;
}
}
}
if(runnable == true)
{
running = true;
Pathfinder p = new Pathfinder(Map);
pathway = p.FindPath(startpoint, endpoint);
running = false;
}
}


I don't know why 2 people voted to close this as I think it is a legitimate questions that isn't more localized than XNA and threading

Starting a new thread is relatively expensive and might cause a tiny bit of jittering when you only have 15ms to build a new frame. The best way to overcome this is by using the a thread pool. Here you park a few threads so that you don't have the overhead of creating a new one every time you need one to do some work. You can roll your own thread pool to get maximum control but .NET also has a built in thread pool ThreadPool that also does so managing so that you dont get too much threads running at once.

What is also important, and for me that wasn't really clear from your example, is that your thread doesn't have to finish in the same update loop iteration. Don't wait for the thread to finish. Just check every frame if the path is ready and use it when it is.

Lastly take care to make your code thread safe and make sure there is as little interaction between threads as possible (safer and faster).

• +1 for Roy T. answer. I also suggest to have a look at this page : albahari.com/threading which just contains everything you ever wanted to know about threading in C#. – tigrou Nov 21 '12 at 14:45

What i´ve learnd of threading is that you create one or more thread(s), and run it parrallel with the game, nonstop. and when you need data there procsed, you simply push a job (a function with data) to that thread and it will take care of it, and when it´s done doing that it just keeps running. That way you dont have to start threads more thane once. and you can push plenty of data there. and if you need an absolut finish state at a specific frame, you could go with some sync callback in the job function.

and as Roy T. states, make your code thread safe. it´s wery important.