Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this program and in it I want to print a string but after some delay........I did some research and found out that I need to use the timer but the problem is I dont know how to use it I have tried to find out how to use timers but i dont understand them as they are high-level and i am only a beginner so I wanted to know if there is some other method to do so....please help.......Thanks in advance.........



Regards,
Ahsan Naveed.
Posted
Comments
ZurdoDev 23-Aug-13 9:23am    
The other method would be to write a loop but that is a terrible way. I would suggest you find a tutorial or read the docs on timers since that is the right way to do it.
Ahsan98 23-Aug-13 9:29am    
I tried to use loop like this

for(int a=0;a==9999;a++){
}
textbox1.text="Hello";

this is only an example the real code i didnt upload because I think it would be too large and thus inappropriate to write.........
ZurdoDev 23-Aug-13 9:32am    
It's a terrible way to do it. Years ago processors were slow enough that you could do it that way but as processors get faster and faster the loop won't even cause a delay. Why do you need to wait?
Ahsan98 23-Aug-13 9:35am    
i need to wait in the real program not here this was just an example i didnt write the real code b/c it was too long........
Ahsan98 23-Aug-13 9:33am    
hi i tried using loops but i didnt work.......

for(int a=0;a==999999;a++){}
label1.text==hello;
but the result still comes quickly.........i dont know what to do please give me some detail on how to use a loop..........timers are a little above level and would take mounths untill i get a better understanding but i nee it now please help.........................

A Timer is the right way to do it, and they are a lot easier to use than it looks! :laugh:
All you need to do is create a Timer object, initialize it, and add a handler method.

Create:
C#
private Timer myTimer = new Timer();

Initialize:
C#
myTimer.Interval = 250;
myTimer.Tick += new EventHandler(myTimer_Tick);
myTimer.Start();

Handler method:
C#
void myTimer_Tick(object sender, EventArgs e)
    {
    // Do your code here
    }
Whatever code you put inside the myTimer_Tick method will be executed every 250 milliseconds - or 4 times a second (that's what the value I put in the Interval property causes)

So if you want to print a string once, after a delay of ten seconds, then:
C#
private Timer myTimer = new Timer();

private void myForm_Load(object sender, EventArgs e)
    {
    myTimer.Interval = 10000;
    myTimer.Tick += new EventHandler(myTimer_Tick);
    }

private void myButton_Click(object sender, EventArgs e)
    {
    myTimer.Start();
    }

void myTimer_Tick(object sender, EventArgs e)
    {
    Console.WriteLine("Hello");
    myTimer.Stop();
    }
If you don't add the "Stop" call, it will repeat until your application ends.
 
Share this answer
 
Comments
Ron Beyer 23-Aug-13 10:31am    
+5 for the right way!
Ahsan98 23-Aug-13 10:39am    
Thanks that helped however could you do me a favor by telling me what this line does
myTimer.Tick += new EventHandler(myTimer_Tick);
OriginalGriff 23-Aug-13 10:54am    
It adds a handler to an event: specifically it says that when the myTimer Interval has passed, the method myTimer_Tick should be called.
This is the way you attach a handler to any event - even your myButton_Click method is attached to the Click event in exactly that way - just you don't normally see it because it is in the InitializeComponent method that is called from your form constructor and "hidden" in the myForm.Designer.cs file.
Ahsan98 24-Aug-13 9:26am    
thanks very much.
OriginalGriff 24-Aug-13 9:40am    
You're welcome!
If you really don't want to use a timer, you can do something like this:

C#
public static class Main()
{
    Console.WriteLine("Delaying for 5 seconds:");

    for (int i = 0; i < 500; i++)
        System.Threading.Thread.Sleep(10);

    Console.WriteLine("Back to work!");
}


Why the for loop?
Sleep blocks the main thread and does not perform the application message pump. Short delays (1-3 seconds) work fine (because it takes longer for the OS to detect the application isn't responding), but longer delays will cause the operating system to detect a non-responding application. Using a for loop allows the main application message pump to respond to the operating system and keep the "application not responding" or the screen from being greyed out from happening.

But I will say that Timer really is the proper way to do this. From the above explanation you can see that using Sleep has its disadvantages, and the only real valid place to use the Sleep is in threads that you create that you want to suspend while waiting for something to happen outside of the thread's control.
 
Share this answer
 
v2
Comments
Ahsan98 23-Aug-13 10:27am    
Yes I know that but this only works for console in win form the app gets held I want it for win apps........so please help..........
Ron Beyer 23-Aug-13 10:30am    
The same would work for windows forms apps, but would cause your application to become non-responsive in that time. You would have to add in the for-loop an Application.DoEvents() call to make sure the UI stays responsive, but would cause a longer delay than the 5 (or whatever) seconds since Application.DoEvents() basically forces the message pump to run. Take the other solution though, that's the proper way to do it.
Ahsan98 24-Aug-13 23:48pm    
please give a bit more explanation on how to do this i am only a beginner so tell me exactle how and what do i do in a for loop with application.doevent so please help.....thanks in advance

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