![]() |
|
[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)Code new Thread(()=>{ ...AI... });* - neiqhtworks - Oct 24, 2013 (Oct 24, 2013 12:01 PM)xSplit Wrote: You need something like this? 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 |