Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to know how I can do some action again and again without something to happen like mousemove for example i want to create random numbers till i close the program.

EDIT:
When I'm trying to put the while(true) the program cannot be started i just want to change picture location by random numbers and i want to do this until the program close
Posted
Updated 4-Apr-11 23:54pm
v2

Create a thread which will generate random number for you .... so your UI will not be blocked. abort the thread when exiting from the form.
Or use a timer event if you want to generate the Random number in timely fashion...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Apr-11 11:52am    
Basically good, but -- not a timer, please! Why? Timers are best avoided.
My 4.
--SA
C#
while (true)
{
    //some code here
}


That's an infinite loop


Edit:
Let me expand on that a bit....if this is a Winforms app and you execute this on the UI thread, you'll block your UI. Either execute on a background thread or make the loop exit at some point

You can force the loop to exit by using a break; statement if the loop meets some condition
 
Share this answer
 
v2
Comments
Olivier Levrey 5-Apr-11 6:17am    
Op commented: When I'm trying to put the while(true) the program cannot be started i just want to change picture location by random numbers and i want to do this until the program close
You can give the loop command for this action....
Do-While can be the best method....
 
Share this answer
 
You should never ever use loop in order to delay some thread. This is called spin-wait and should not be tolerated. First, you need to create a separate thread. If you need to keep the thread from execution until something happen, use System.Threading.EventWaitHandle.

When a thread calls the blocking method EventWaitHandle.WaitOne it is put to a wait state by the OS: switched off and never scheduled back to execution before awaken, thus spending zero CPU time. It can only be awaken by EventWaitHandle.Set or System.Threading.Thread.Abort called by other thread.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900