Howdy, Stranger!

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

Categories

Delay function in c#

im tooim too Member Posts: 42
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
«1

Comments

  • WEBMASTERWEBMASTER Member Posts: 549
    : 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]

  • iwilld0itiwilld0it Member Posts: 1,134
    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
    :

  • WEBMASTERWEBMASTER Member Posts: 549
    : 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]

  • dale58dale58 Member Posts: 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
    :
    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
  • im tooim too Member Posts: 42
    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

  • WEBMASTERWEBMASTER Member Posts: 549
    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]

  • jamsek19jamsek19 Member Posts: 2
    : 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
  • guanzhaoguanzhao Member Posts: 2
    : : 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?
  • balagosabalagosa Member Posts: 1
    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*

  • JonathanJonathan Member Posts: 2,914
    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.");
«1
Sign In or Register to comment.