Hack Forums
[C#] Alternative to Thread.Sleep - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Alternative to Thread.Sleep (/showthread.php?tid=3823853)



Alternative to Thread.Sleep - neiqhtworks - Oct 24, 2013

Is there an alternative to this?

I'm making a game where you're battling an ai and everything works how it should but the ai attacks as soon as you click your spell (it's turn based), is there a way to do something like thread.sleep to pause it for a second? I thought about using a timer but I'm not sure how I would integrate it with my methods..

would using a background worker pause the script but not the UI itself?


* - RedWaters - Oct 24, 2013

You need something like this?
Code
static void SleepFor(int seconds)
  {
    DateTime start = DateTime.Now;
    while (DateTime.Now.Subtract(start).TotalSeconds < seconds) ;
  }
Anyway i think your problem is that you don't use different threads, for example one for the ai and the main thread for the ui of the program
Code
new Thread(()=>{ ...AI... });
//or
new Thread(()=>YourGameManager.InitAi());



* - neiqhtworks - Oct 24, 2013

(Oct 24, 2013 12:01 PM)xSplit Wrote:  You need something like this?
Code
static void SleepFor(int seconds)
  {
    DateTime start = DateTime.Now;
    while (DateTime.Now.Subtract(start).TotalSeconds < seconds) ;
  }
Anyway i think your problem is that you don't use different threads, for example one for the ai and the main thread for the ui of the program
Code
new Thread(()=>{ ...AI... });
//or
new Thread(()=>YourGameManager.InitAi());

Yeah I wasn't using threading, I'll try that.


* - Silicon - Oct 24, 2013

There isn't a non-blocking Sleep method that exists without using multiple threads because that wouldn't make sense. It's worth using different threads as xSplit said, or using something like XNA where that's all handled for you. Even then Thread.Sleep is a poor idea, just use a counter to decrement something every in-game tick and only do the spell once it equals zero.


* - iVision - Oct 24, 2013

(Oct 24, 2013 04:36 PM)Silicon Wrote:  There isn't a non-blocking Sleep method that exists without using multiple threads because that wouldn't make sense. It's worth using different threads as xSplit said, or using something like XNA where that's all handled for you. Even then Thread.Sleep is a poor idea, just use a counter to decrement something every in-game tick and only do the spell once it equals zero.

Yes something like that your main loop of the game, if you use XNA you should do it in the Update method.
I would use a Timer or a Task to restore the state (things I could think of):
Code
new System.Threading.Timer((o) => skill.Enable(), null, skill.Delay, Timeout.Infinite); //note that its a Threading.Timer not a Forms.Timer

new Task(async () =>
    {
    await Task.Delay(skill.Delay);

    skill.Enable();
    }).Start(); //.NET 4.5.1 (or 4.5 cannot remember sorry)

    new Task(() =>
    {
    Thread.Sleep(skill.Delay);

    skill.Enable();
    }).Start(); //.NET 4.0

    new Thread(() =>
    {
    Thread.Sleep(skill.Delay);

    skill.Enable();
    }).Start(); //.NET 2.0 (The lamba makes it 3.5 though)