Delay function in c#

Hi,
As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
for(i=0;i<10;i++){printf("%d",i);delay(100);}

Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
Thanks

Comments

  • : Hi,
    : As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
    : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    :
    : Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
    : Thanks
    :


    I usually use Thread.Sleep(xxxx) where XXX is in miliseconds.

    you need to add "Using System.Threading" as well.

    Also I usually introduce Application.DoEvents() here and there to make the GUI more responsive. adding Application.DoEvents() can also slow it down slightly.







    [blue][italic][b]/WEBMASTER[/b][/italic][/blue]

  • You can clock watch as well ...

    Use Environment.TickCount property. Something like ...

    [code]
    function Delay(int ms)
    {
    int time = Environment.TickCount;

    do while(true)
    if(Environment.TickCount - time >= ms) return;
    }
    [/code]

    : Hi,
    : As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
    : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    :
    : Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
    : Thanks
    :

  • : do while(true)
    : if(Environment.TickCount - time >= ms) return;

    Is not so good because you prevent the CPU from running other tasks during the time the application waits. Also you will consume 100% of the CPU here.

    using Thread.sleep allows other tasks to run instead.

    [blue][italic][b]/WEBMASTER[/b][/italic][/blue]

  • : Hi,
    : As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
    : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    :
    : Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
    : Thanks
    :
    From the original webpage http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=269711&SiteID=1

    System.Threading.Thread.Sleep(5000);

    this codes make your application waiting for 5 seconds
  • Oh I see
    System.Threading.Thread.Sleep(int);
    use as delay();

    But what is the function/working of "timer" in visual C# .Net?
    before ...Thread.Sleep().I was trying to use the "timer" as Delay() .

    Anyway..........Thanks all of you

  • Timer is a bit different because the current "thread" does not pause its execution, it continues.

    Timer uses events/delegates and the timer function is executed in a different thread independently of the thread that created it.

    "The timer method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.
    "


    see http://msdn2.microsoft.com/en-us/library/system.threading.timer.aspx

    You use the timer to execute code at regular intervalls and thread.sleep to pause the current execution thread.


    [blue][italic][b]/WEBMASTER[/b][/italic][/blue]

  • : Hi,
    : As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
    : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    :
    : Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
    : Thanks
    :


    Hi.
    in C# you can use Sleep method of Thread class:


    using System.Threading;

    Int32 miliseconds_to_sleep = 2000;
    Thread.Sleep(miliseconds_to_sleep);




    Best regards
    Andreo
  • : : Hi,
    : : As i know that ,for delay the any procedure in C-language ,we use the delay() function like that
    : : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    : :
    : : Also i know that in visual C# ,we use timer ,but how do i use timer as delay() ?
    : : Thanks
    : :
    :
    :
    : I usually use Thread.Sleep(xxxx) where XXX is in miliseconds.
    :
    : you need to add "Using System.Threading" as well.
    :
    : Also I usually introduce Application.DoEvents() here and there to
    : make the GUI more responsive. adding Application.DoEvents() can also
    : slow it down slightly.
    :
    :
    :
    :
    :
    :
    :
    : [blue][italic][b]/WEBMASTER[/b][/italic][/blue]
    :
    :

    can i use Threading.Sleep(XXXX) inside a "for" loop? I 've tried something like the following:

    for (i=0;i<=10;i++)
    {
    label.Text = i.ToString();
    Threading.Sleep(100)
    }

    it doesn't work as the result do not count slowly to 9 but instead delayed for 100ms and simply jumped to 9. Can you help me solve this issue?
  • hi, i am new here. i used the Thread.Sleep and it isnt working for me [u]correctly[/u]

    here is my code:

    c.Text = que.Proprity_No.ToString();

    c.ForeColor = Color.White;
    Thread.Sleep(4000);
    c.ForeColor = Color.Blue;

    (What it is supposed to do:) *Start* Original of Blue Color Font. then it changes to White Color Font. after 4 seconds, change back to blue. /it is only suuposed to make [b]1label[/b] invicible/ *End*

    (What it is doing right now:) *Start* A blank screen for 4 seconds then the final result is a blue font. *End*

    Blank Screen - there are buttons in my program. the labels are all blank...Not White but blank as in it didnt trigger/load yet; the screen.

    Note: i know its supposed to execute the white first, but it isnt. In my own, it executes the Sleep first then the color changing.

    I tried this one:
    Thread.Sleep(4000);
    c.ForeColor = Color.White;

    Result: *Start* blank screen for 4 seconds. then the white labelshows up *End*

  • Try an:

    Application.DoEvents();

    Before you call Sleep.

    Jonathan
    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
  • : Hi,
    : As i know that ,for delay the any procedure in C-language ,we use
    : the delay() function like that
    : for(i=0;i<10;i++){printf("%d",i);delay(100);}
    :
    : Also i know that in visual C# ,we use timer ,but how do i use timer
    : as delay() ?
    : Thanks
    :

    [code]

    public static DateTime PauseForMilliSeconds( int MilliSecondsToPauseFor )
    {


    System.DateTime ThisMoment = System.DateTime.Now ;
    System.TimeSpan duration = new System.TimeSpan( 0, 0, 0, 0, MilliSecondsToPauseFor ) ;
    System.DateTime AfterWards = ThisMoment.Add( duration ) ;


    while ( AfterWards >= ThisMoment )
    {
    System.Windows.Forms.Application.DoEvents() ;
    ThisMoment = System.DateTime.Now ;
    }


    return System.DateTime.Now ;
    }


    [/code]

    Cut and Paste it in your Files Call it from Where Ever
    Even From Loops The Time you Want to Pause For can
    Even be Days; The Advantage of this is That
    It Will WOrk According to the Machine you are running on
    Because it calculats the Time Laspe From the System Clock
    SO every Time It Is the Same.
  • thanks. it works perfectly.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories