Hack Forums
[C#] C# [ TUT ] Sleep ur program without stopping the whole program! [/TUT] - 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#] C# [ TUT ] Sleep ur program without stopping the whole program! [/TUT] (/showthread.php?tid=2411450)

Pages: Pages 1 2


C# [ TUT ] Sleep ur program without stopping the whole program! [/TUT] - pimpie1995 - Apr 17, 2012

Hey guys!
I have searched for this for a long time and after i tried somethings i figured out i found a way to Sleep ur program without it will stop the whole program

we all know the Thread.Sleep Method, but that sucks

i made this little count down;
[Image: usH7F.gif]

Syntax / Code:
Quote: int counter = 0

private void timer1_Tick(object sender, EventArgs e)
{
if (counter < Convert.ToInt32(tbSeconds.Text))
{
counter++;
lblCounter.Text = Convert.ToString(counter);
}
else
{
tbWorked.Text = "WORKED!";
timer1.Stop();
}
}

private void bMakeitrain_Click(object sender, EventArgs e)
{
timer1.Start();
}


I think if you already are advanced with coding in C#, you should know now how to "pause" your script with this method :)

Have fun with trying guys and good luck!


* - iVision - Apr 17, 2012

I found the best way, multithreading!


* - pimpie1995 - Apr 17, 2012

could you learn me that :x?, im sure there are still 100th more ways, but i just wanted to share with you guys :p


* - BaussHacker - Apr 17, 2012

(Apr 17, 2012 09:49 AM)iVision Wrote:  I found the best way, multithreading!

A timer is also a thread lol. πŸ˜‰

Also saying the Thread.Sleep method suck...
It's great when working with multithreading, because you won't slip your cpu up by putting one Thread.Sleep(1) inside your while loop within your thread.

You're obviously new, I can see that and for that reason you shouldn't throw such statement and your program is not sleeping at all...


* - pimpie1995 - Apr 17, 2012

(Apr 17, 2012 11:11 AM)Ԍray Wrote:  A timer is also a thread lol. πŸ˜‰

Also saying the Thread.Sleep method suck...
It's great when working with multithreading, because you won't slip your cpu up by putting one Thread.Sleep(1) inside your while loop within your thread.

You're obviously new, I can see that and for that reason you shouldn't throw such statement and your program is not sleeping at all...

Yes i am new, i am studing C# on school now and i realy like to work with it :) tnkx for the feedback


* - BaussHacker - Apr 17, 2012

That's just great :) If you have any questions or need help, then feel free to PM me :)


* - DragQueen - Apr 17, 2012

its call Multithreading.
and the best thing its that timer1_Tick
running in the main thread


* - SpriteZ - Apr 17, 2012

A while loop would be better, but multithreading is still a great option.


* - pimpie1995 - Apr 17, 2012

(Apr 17, 2012 02:38 PM)SpriteZ Wrote:  A while loop would be better, but multithreading is still a great option.

you cant pause ur action with a while loop... also when a while loop is to long, it will freeze ur program until its finished


* - BaussHacker - Apr 18, 2012

(Apr 17, 2012 02:38 PM)SpriteZ Wrote:  A while loop would be better, but multithreading is still a great option.

Putting the loop in a new thread, would be better. πŸ˜…


* - Volim te - Apr 18, 2012

Bad coding sorry less use that Convert.ToInt32() in a try/catch block.


* - Maiki - Apr 18, 2012

Uhm...why not use a backgroundworker ?


* - DragQueen - Apr 18, 2012

(Apr 17, 2012 11:44 PM)pimpie1995 Wrote:  you cant pause ur action with a while loop... also when a while loop is to long, it will freeze ur program until its finished

if this while ... in diffrent thread.
it can be done.


* - blazzored - Apr 18, 2012

DateTime start = DateTime.Now;
while (DateTime.Now.Subtract(start).Seconds < 10)
{
Application.DoEvents();
System.Threading.Thread.Sleep(1);
}

I win.


* - BaussHacker - Apr 18, 2012

(Apr 18, 2012 10:37 PM)blazzored Wrote:  DateTime start = DateTime.Now;
while (DateTime.Now.Subtract(start).Seconds < 10)
{
Application.DoEvents();
System.Threading.Thread.Sleep(1);
}

I win.

No you don't. Yours will still sleep the app.

I win.

Code
System.Threading.Thread newThread = new System.Threading.Thread(new ThreadStart(delegate {
int count = 10;
while (++count < 10)
{
//lolwut
    System.Threading.Thread.Sleep(1);
}
}));
newThread.Start();