C
C#4d ago
Yesn't

✅ Why won't the loop loop?

public class Game
{
public Task gameloop = Task.Factory.StartNew(() =>
{
// new world
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
namespace OpenWarfare
{
internal class Program
{
static void Main(string[] str)
{
Game game = new Game();
game.gameloop.Start();
}
}
}
public class Game
{
public Task gameloop = Task.Factory.StartNew(() =>
{
// new world
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
namespace OpenWarfare
{
internal class Program
{
static void Main(string[] str)
{
Game game = new Game();
game.gameloop.Start();
}
}
}
I'm using the console template + old coding style. I expected the game to run in a loop, however the game exited. The code above is my Program.cs btw
10 Replies
Aaron
Aaron4d ago
starting a task does not wait for that task to finish (and it was already started anyway)
Yesn't
Yesn'tOP4d ago
so how am i gonna workaround ts?
Fayoka
Fayoka4d ago
You’re creating the Task and starting it immediately with Task.Factory.StartNew. That means the loop is already running by the time the Game constructor finishes. 2 options Option 1: Use Task.Factory.StartNew (don’t call Start())
public class Game
{
public Task gameloop;

public Game()
{
gameloop = Task.Factory.StartNew(() =>
{
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
}
public class Game
{
public Task gameloop;

public Game()
{
gameloop = Task.Factory.StartNew(() =>
{
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
}
or Option 2: Use new Task(...) and then Start()
public class Game
{
public Task gameloop;

public Game()
{
gameloop = new Task(() =>
{
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
}
public class Game
{
public Task gameloop;

public Game()
{
gameloop = new Task(() =>
{
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
}
}
Yesn't
Yesn'tOP4d ago
ok no it wont work
namespace OpenWarfare
{
internal class Program
{
static void Main(string[] str)
{
Task gameloop = new Task(() =>
{
// new world
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity.Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
gameloop.Start();
}
}
}
namespace OpenWarfare
{
internal class Program
{
static void Main(string[] str)
{
Task gameloop = new Task(() =>
{
// new world
World world = new World(10, 10);
world.EntityManager.Spawn(0, 1, new Entity.Entity() { x = 0, y = 1, movement = new Vector2(5, 5) });
while (true)
{
world.tick();
}
}, TaskCreationOptions.LongRunning);
gameloop.Start();
}
}
}
jcotton42
jcotton424d ago
Never use new Task. And you’re still not waiting on the task. As a background thing, the runtime doesn’t wait for them to finish before exiting. Use an async signature for Main, and await the Task.
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Evyr
Evyr4d ago
out of curiosity, why should you never use new Task?
jcotton42
jcotton424d ago
I mean, mostly there’s no reason to. StartNew and Run cover the cases fine.
Unknown User
Unknown User4d ago
Message Not Public
Sign In & Join Server To View
Yesn't
Yesn'tOP3d ago
alr ikr just didnt know it's applicable for Main(string[]) and how will it change thank yall though

Did you find this page helpful?