SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# wait for a function to execute and delay

    1. How can I create a delay in C#.

    I know Thread.Sleep(0) but it is blocking my GUI.

    2. Also I need to pause the execution of the normal flow until a function return a result.

  2. #2
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,625
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    What you really want is the async extensions in C# 5.0, but those are not here yet.

    What you want to look at today is background workers as they do this sort of thing without blocking the GUI.

  3. #3
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,873
    Mentioned
    44 Post(s)
    Tagged
    0 Thread(s)
    Do some research on Threading. As you will want to run this in a separate thread

  4. #4
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wwb_99 View Post
    What you really want is the async extensions in C# 5.0, but those are not here yet.

    What you want to look at today is background workers as they do this sort of thing without blocking the GUI.
    What?! When is C# 5.0 dropping? It's becoming a rat-race.

  5. #5
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    What I usually do if I need to disable my GUI while something is running is... I disable my GUI while something is running. =p

    Just set your Form's Enabled file to false when it starts and true when it's ready to go again. That'll disable your whole GUI.

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by samanime View Post
    What I usually do if I need to disable my GUI while something is running is... I disable my GUI while something is running. =p

    Just set your Form's Enabled file to false when it starts and true when it's ready to go again. That'll disable your whole GUI.
    Wouldn't the application still appear to hang from the user's point of view?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Well, when I do it it's always initiated by user action, so since it disables the controls (visually too, grayed out) it seems like it should. It doesn't just randomly "hang". Before I had it disable and gray out it did, which is why I started disabling it.

  8. #8
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    When you do that, does Windows 7 leave the program alone, or does it still like to show its "searching for solution" window?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  9. #9
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Ah, that.

    Anything I know will take a long time, I set it up on a BackgroundWorker (or thread it properly, depending on the scale of the project) if I think it might make Windows think it's hung. For example, I wrote a program a few weeks ago to process a ridiculous number of server logs into a database then build a cache to make analyzing the data quicker.

    The disabling everything is only for the users' benefit.

  10. #10
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Ok, that's what I was trying to get at.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  11. #11
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for your answers.

    Seems that the solution is the "BackgroundWorker" and I searched for tutorials on Google and could work for me.

    What about a 'delay' function like in C++, can be implemented in C# ?

  12. #12
    SitePoint Guru pufa's Avatar
    Join Date
    Oct 2004
    Location
    Portugal, Lisboa
    Posts
    947
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe Task<T> would be enough...

    Task(TResult) Class (System.Threading.Tasks)
    Ciao, Rui...

  13. #13
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    Thanks for getting me to spit out the right answer, Force Flow. =p I always forget that Windows gets a bit antsy with those things until it happens.

    The best way I can think of to implement a delay without Windows freezing would be to create a background worker with a function that just has a while loop that loops until enough time has passed (so get the start time then loop until (start + delay >= now), then return.

    If you wanted to it disable everything, just wrap the delay in with the Enabled = False/True statements.

    You could also make it a bit more flexible by giving it a function to call when it was done delaying (to enable everything or something).

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2010
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was thinking on something similar, get the time, add one minute, then use a while loop until the actual time is equal with the starting time + delay.

    I'm a newbie and I have some problems adding the delay. Can you help me with the code ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •