Results 1 to 15 of 15

Thread: My Wait Function

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    My Wait Function

    If you dont want to use Threading.Thread.Sleep then you may want to use this.
    VB.NET Code:
    1. Private Sub Wait(ByVal Milliseconds As Integer)
    2.         Dim sw As New Stopwatch
    3.         sw.Start()
    4.         Do While sw.ElapsedMilliseconds < Milliseconds
    5.             Application.DoEvents()
    6.         Loop
    7.         sw.Stop()
    8.     End Sub

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: My Wait Function

    Quote Originally Posted by Emcrank View Post
    If you dont want to use Threading.Thread.Sleep then you may want to use this.
    VB.NET Code:
    1. Private Sub Wait(ByVal Seconds As Integer)
    2.         Dim WaitTimer As New Timer
    3.         AddHandler WaitTimer.Tick, AddressOf WaitTimerTick
    4.         WaitTimer.Interval = Seconds * 1000
    5.         WaitTimer.Start()
    6.     End Sub
    7.     Private Sub WaitTimerTick(ByVal sender As Object, ByVal e As System.EventArgs)
    8.         'CODE HERE
    9.         DirectCast(sender, Timer).Stop()
    10.     End Sub
    So what's the purpose of this code when it doesn't actually wait synchronously.
    e.g. execute somecode, wait for a few seconds, continue executing, wait for another few seconds, and continue executing rest of the code. How do you do that?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: My Wait Function

    I didn't know how to make it do that so all it does is waits the seconds you want it to then you put the code you were going to put after the Wait(<Seconds>) in the WaitTimer Tick. You just duplicate the wait function if you need to do 2 waits.

    EDIT: If you know a better one that acctually makes program wait then resumes executing code, That would be great if you could tell me it

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: My Wait Function

    Quote Originally Posted by Emcrank View Post
    I didn't know how to make it do that so all it does is waits the seconds you want it to then you put the code you were going to put after the Wait(<Seconds>) in the WaitTimer Tick. You just duplicate the wait function if you need to do 2 waits.

    EDIT: If you know a better one that acctually makes program wait then resumes executing code, That would be great if you could tell me it
    No what I meant is:

    Code:
    Sub DoSomething
        'some code lines to execute here
        Wait (SomeSeconds)
        'some code lines to execute here
        Wait (AnotherFewSeconds)
        'finally execute this part of the procedure.
    End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: My Wait Function

    As long as the code you're stopping for x amount of time isn't on the same thread as the other code you're waiting for then System.Threading.Thread.Wait(TimeInMiliseconds) should work fine for ya.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: My Wait Function

    There is a way to wait using Application.DoEvents and a loop, but it uses your CPU constantly. Apart frm that, there's no way to keep the UI responsive while running synchronously on a UI thread

  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Wait Function

    I dont mean to sound harsh or anything and its good that you are posting things here to try and help others out but all that code does is uses a timer - it doesnt do anything more. Anyone can drag and drop a timer onto their form and get the exact same functionality, so I dont quite see the point in it...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8

  9. #9

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Wait Function

    Quote Originally Posted by Emcrank View Post
    Brand new wait function. Works like a charm. No need for this to be deleted now
    I'm afraid it doesnt work like a charm - it uses up 25% of my CPU (which is a quad core CPU so it would be 100% if I only had 1 core) and the form is completely dead while its doing that. You could achieve the same effect but without the CPU thrashing by using Threading.Thread.Sleep
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: My Wait Function

    Thats weird :/ im only on a laptop and it powers through my forms
    Also the thing i find with Threading.Thread.Sleep is the code after it sometimes doesn't work properly

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: My Wait Function

    In what way doesnt it work properly? I've never had any problems with any code after Thread.Sleep and not heard of anyone else having any either. Maybe you should start a new thread in the VB.NET forum about it?

    As for your laptop being able to display forms and them working fine once you have called this wait function, I would be very surprised if that is really the case - I suspect you might be just setting it to only wait for a very short period of time (like 200 milliseconds or something) so the freeze isn't noticeable. if you tell it to wait for 10 seconds (10000 milliseconds obviously) then does the form completely freeze and hog all the CPU? I would totally expect it to because what you are doing is just a constant loop, so the processor doesn't have time to do anything else like repaint the form or handle user input (even though you are calling Application.DoEvents).
    Anyway, even if it does somehow work on some machines, you would still be much better off just using a timer if you want the form to still be responsive. I know you were using a timer originally but I never said that was a bad idea, I just said there wasnt much point in a codebank thread showing how to use a timer.

    EDIT: Sorry it appears I was wrong, the form does respond (no idea why it didnt the first time I tested it) but it does still use a full core's worth of CPU, so I would definitely still advise against it. Plus I have seen some weird side effects of using Application.DoEvents repeatedly in the past as well, like code in event handlers being run multiple times when they shouldn't have been. I think if you want something to happen after X number of milliseconds but you want your form to still be responsive in the mean time then a Timer is the only way to go really (though you could make your own timer using multi threading there would not be much point as the windows forms one works perfectly well).
    Last edited by chris128; Jul 8th, 2010 at 05:40 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: My Wait Function

    When i tested it i tested it for 5000ms so 5 seconds and it was ok. Also the threading.thread.sleep freezes the whole application. Like so if you have a timer running and you want it to still run while you freeze one part of code. I would have to make a new thread in the application and it takes ages when you have to delegate everything. Ill just use this for myself i wont put it in anything anyone else might use.

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: My Wait Function

    Since you are on the UI thread, if you make the thread sleep for a long time, the UI will become unresponsive. The recommended way is to work on a separate thread.

    Anyways, if you want to work on the UI thread itself, you can insert a small sleep in your wait funciton. 100ms works absolutely fine.

    vb Code:
    1. Private Sub Wait(ByVal Milliseconds As Integer)
    2.     Dim sw As New Stopwatch
    3.     sw.Start()
    4.     Do While sw.ElapsedMilliseconds < Milliseconds
    5.         Application.DoEvents()
    6.         Threading.Thread.Sleep(100)
    7.     Loop
    8.     sw.Stop()
    9. End Sub
    I tested for 10 seconds interval on my Dual Core CPU.
    Without that Threading.Thread.Sleep call there the CPU usage is above 50&#37; during the entire wait period. With that sleep inserted there, its between 1% to 2%. And the UI still stays responsive. The disadvantage is that Wait for less than 100 ms won't work. So in that case you can remove the sleep as it would be virtually useless.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15

Posting Permissions

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



Click Here to Expand Forum to Full Width