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;
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!
I found the best way, multithreading!
could you learn me that :x?, im sure there are still 100th more ways, but i just wanted to share with you guys :p
(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...
(Apr 17, 2012 11:11 AM)BaussHacker 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
That's just great :) If you have any questions or need help, then feel free to PM me :)
its call Multithreading.
and the best thing its that timer1_Tick
running in the main thread
A while loop would be better, but multithreading is still a great option.
(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
(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. 😅
Bad coding sorry less use that Convert.ToInt32() in a try/catch block.
DO NOT DEAL WITH THIS MEMBER
Uhm...why not use a backgroundworker ?
(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.
DO NOT DEAL WITH THIS MEMBER
DateTime start = DateTime.Now;
while (DateTime.Now.Subtract(start).Seconds < 10)
{
Application.DoEvents();
System.Threading.Thread.Sleep(1);
}
I win.
(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();
|