#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    81
    Rep Power
    9

    Timer delay in C#


    I'm trying to create a 2 second delay between two lines being executed, but haven't had much sucess in finding any examples on how the Timer can be used in that regard.

    I came across System.Threading.Thread.Sleep(2000), but that didn't seem to execute at all.

    I also tried this:
    Code:
            private void dealTimer()
            {
                // create the timer set the event handler
                Timer timer = new Timer();
    
                // set the event handler
                timer.Elapsed += new
                ElapsedEventHandler(OnElapsed);
    
                // set the timer properties
                timer.Interval = 2000;
                timer.AutoReset = true;
    
                // start the timer
                timer.Start();
            }
    
    
            void OnElapsed(Object sender, ElapsedEventArgs e)
            {
                // on timeout this code is executed
                int bork = 0;
            }
    But I get an error when I try to comple the program: The type or namespace name 'ElapsedEventArgs' could not be found (are you missing a using directive or an assembly reference?)

    Any ideas? Thanks
  2. #2
  3. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    Use Thread.Sleep() instead.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    81
    Rep Power
    9
    The name 'Thread' does not exist in the current context

    That doesn't even complile.


    This is not a multi-threaded application, so how would this thread sleep method work?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Posts
    297
    Rep Power
    20
    Use System.Threading, and you don't need a multithreaded program to use System.Threading. When you startup the program its essentially spawns a single thread for you in which everything runs inside of.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    81
    Rep Power
    9
    Code:
    private void tasksToRun(){ 
    
    if(blah){ 
    //execute some stuff 
    } 
    
    System.Threading.Thread.Sleep(2000);
    
    if(blah2){ 
    //execute some stuff 
    } 
    
    System.Threading.Thread.Sleep(2000);
    
    if(blah3){ 
    //execute some stuff 
    } 
    
    
    }

    If I try that, it breaks my program. The if statements don't seem to execute at all, or it seems like the program picks and chooses what to run.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    81
    Rep Power
    9
    ok, I think I found a different way to tackle this:

    Code:
    private void dealTimer()
            {
                DateTime dt1 = DateTime.Now;
                int diff = 0;
    
                while (diff < 1)
                {
    
                    DateTime dt2 = DateTime.Now;
                    TimeSpan ts = dt2.Subtract(dt1);
                    diff = (int)ts.TotalSeconds;
                    Application.DoEvents();
    
                }
            }
    It seems to work fine and doesn't break anything.
  12. #7
  13. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    Ugh. Throw that up on coding horror! Trust me, you want to play with sleep some more.

    I'm just kinda curious- what is the delay for, anyway?
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2005
    Posts
    81
    Rep Power
    9
    I was making a blackjack card game, and the cards were being dealt out too fast to see how they were being dealt, so I needed to slow it down a bit.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2006
    Posts
    1
    Rep Power
    0
    Originally Posted by Force Flow
    I'm trying to create a 2 second delay between two lines being executed, but haven't had much sucess in finding any examples on how the Timer can be used in that regard.

    I came across System.Threading.Thread.Sleep(2000), but that didn't seem to execute at all.

    I also tried this:
    Code:
            private void dealTimer()
            {
                // create the timer set the event handler
                Timer timer = new Timer();
    
                // set the event handler
                timer.Elapsed += new
                ElapsedEventHandler(OnElapsed);
    
                // set the timer properties
                timer.Interval = 2000;
                timer.AutoReset = true;
    
                // start the timer
                timer.Start();
            }
    
    
            void OnElapsed(Object sender, ElapsedEventArgs e)
            {
                // on timeout this code is executed
                int bork = 0;
            }
    But I get an error when I try to comple the program: The type or namespace name 'ElapsedEventArgs' could not be found (are you missing a using directive or an assembly reference?)

    Any ideas? Thanks

    You have to include the System.Timers library (using System.Timers), and then declare the variable timer as: System.Timers.Timer timer, and then you can use the ElapsedEventArgs..
    [QUOTE]

IMN logo majestic logo threadwatch logo seochat tools logo