Results 1 to 7 of 7

Thread: Quick Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2006
    Posts
    7

    Quick Question

    I'm new to VB.Net programming (and programming in general) so this is probably a really simple question.

    What is the simplest way to make a piece of code wait for x amount of milliseconds before moving to the next piece of code? Thanks to whoever can help.

  2. #2
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Quick Question

    The CurrentThread can do this.
    VB Code:
    1. System.Threading.Thread.CurrentThread.Sleep(7000)
    VB 2005, Win Xp Pro sp2

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Quick Question

    hello and welcome to the forums
    Well you can either make the thread "go to sleep" using this code:

    VB Code:
    1. Threading.Thread.Sleep(20) 'Sleep 20 miliseconds
    But I guess since you say youre new to VB you probably havnt multithreaded your application, so that line would freeze your application during the "sleep".

    Another thing is to place a timer on your form and setting its interval to the amount of miliseconds to wait. Then you'd just have to set its property Enabled to True when you want to wait. In the tick event of the timer you would place timer1.enabled = false and the other code that you wanted to execute.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quick Question

    Quote Originally Posted by Half
    The CurrentThread can do this.
    VB Code:
    1. System.Threading.Thread.CurrentThread.Sleep(7000)
    I just wanted to point out that this code is incorrect. It will work but only because the IDE fixes it for you when compiling. Thread.Sleep is a Shared method that pauses the current thread. It is supposed to be called on the Thread class itself, NOT on an instance of the Thread class. You don't specify the current thread because the Thread.Sleep method specifically pauses the current thread. It's not possible to pause any thread other than the current. Multi-threading is error-prone enough without allowing threads to halt each other.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Quick Question

    It has no choice but to work since the intellisense suggested it. Thanks for correcting me.
    VB 2005, Win Xp Pro sp2

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Quick Question

    Quote Originally Posted by Half
    It has no choice but to work since the intellisense suggested it. Thanks for correcting me.
    VS 2005 is smarter than VS.NET 2003 in that regard. 2005 would still let you write that code but it would produce a compilation warning telling you that you were accessing a Shared member incorrectly via an instance. That said, you should never call any Shared member on an instance under any circumstances.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Quick Question

    Quote Originally Posted by Half
    It has no choice but to work since the intellisense suggested it. Thanks for correcting me.
    But if you went this route you would have found .Sleep also. Same in 2003/2005

    System.Threading.Thread.Sleep(20)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

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