Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Alternative to Thread.Sleep

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?
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());
(Oct 24, 2013 12:01 PM)RedWaters 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.
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.
ヽ༼ຈل͜ຈ༽ノ
(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)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by SteamMar 12, 2019 04:03 PM 6 415 Mar 12, 2019 04:03 PM
Last Post: Steam
[.NET] by BarretJan 5, 2017 10:29 AM 12 448 Jan 5, 2017 10:29 AM
Last Post: Barret
[.NET] by SIM-2362662Nov 10, 2015 11:48 AM 10 510 Nov 10, 2015 11:48 AM
Last Post: IAmNotHere