delays

This is a discussion on delays within the C# Programming forums, part of the General Programming Boards category; is there a way of delaying a code statement without using threads and not a for loop i.e is there ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    delays

    is there a way of delaying a code statement without using threads and not a for loop

    i.e is there a pre-written class/method ?


    luigi

  2. #2
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Are you talking about a blocking wait without creating another thread?

    Thread.Sleep() will cause the current thread to block. If this isn't what you mean, can you be a little more specific?

    Cheers

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    delays without threads

    yes i meant can one delay execution without creating a thread so i got
    for example:

    code statement;
    code statement;
    delay 1 second here;
    code statement;

    can this be done without threading, is there any classes/methods that dont require creating a thread to execute a delay routine?

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    yes.

    Code:
    System.Threading.Thread.Sleep(delayInMilliseconds);
    This does not create another thread.
    Cheers!

  5. #5
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    delay works but program doesnt

    hi thanks TheColonial for your reply, i want simulate traffic lights changing however with the folowing code the lights still change instantaniously even with 2sec delays between method calls, can anyone explain why? im baffled

    the method im calling is

    Code:
    public void OnOneSecond(Object source, EventArgs e)
    {
    						
    label1.Text = "Simulation time  " + timeIndex++ + "  Seconds";
    if (timeIndex%1==0)
    {
    nveh=trafficControl.CheckNTrafficSensor();
    sveh=trafficControl.CheckSTrafficSensor();
    stextBox.AppendText(sveh + "\r\n");
    ntextBox.AppendText(nveh + "\r\n");
    if(timeIndex%5==0)
    {
    switch(test)
    {
    case 0: 
    {
    System.Threading.Thread.Sleep(2000);
    NorthLightSequence(AMBER);
    System.Threading.Thread.Sleep(2000);					
    NorthLightSequence(RED);
    SouthLightSequence(RED_AMBER);
    System.Threading.Thread.Sleep(2000);
    SouthLightSequence(GREEN);
    test=1;
    }	
    break;
    
    case 1: 
    {
    System.Threading.Thread.Sleep(2000);
    NorthLightSequence(RED_AMBER);
    System.Threading.Thread.Sleep(2000);					
    NorthLightSequence(GREEN);
    SouthLightSequence(AMBER);
    System.Threading.Thread.Sleep(2000);
    SouthLightSequence(RED);
    test=0;
    }
    break;
    }//end switch
    					
    }//end if	
    }
    				
    					
    			
    }
    is it to do with the fact of being in a timer method?
    Last edited by luigi40; 04-07-2005 at 11:21 AM.

  6. #6
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    Hi,

    It could well be. Can you attach your .cs file to the thread so I can see how your program is set up? I should be able to help you if I can see the full code.

    Cheers.

  7. #7
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    changed

    Quote Originally Posted by TheColonial
    Hi,

    It could well be. Can you attach your .cs file to the thread so I can see how your program is set up? I should be able to help you if I can see the full code.

    Cheers.
    Hi TheColonial, i have totally modified the code now so that question is ir-relevant.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating waits and processing delays
    By ulillillia in forum Windows Programming
    Replies: 16
    Last Post: 12-24-2006, 04:52 PM
  2. time Delays and Random functions
    By markphaser in forum C++ Programming
    Replies: 17
    Last Post: 02-20-2006, 07:10 PM
  3. Quick Delays
    By bigB8210 in forum C Programming
    Replies: 1
    Last Post: 07-10-2003, 03:06 PM
  4. precision delays
    By marcdawson in forum C Programming
    Replies: 5
    Last Post: 11-08-2002, 11:03 AM
  5. Putting Timer/Time Delays into your programs
    By newbie in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2001, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21