|
-
Dec 11th, 2006, 02:33 PM
#1
Thread Starter
New Member
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.
-
Dec 11th, 2006, 02:47 PM
#2
Re: Quick Question
The CurrentThread can do this.
VB Code:
System.Threading.Thread.CurrentThread.Sleep(7000)
-
Dec 11th, 2006, 02:51 PM
#3
Re: Quick Question
hello and welcome to the forums
Well you can either make the thread "go to sleep" using this code:
VB Code:
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.
-
Dec 12th, 2006, 02:08 AM
#4
Re: Quick Question
 Originally Posted by Half
The CurrentThread can do this.
VB Code:
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.
-
Dec 12th, 2006, 08:59 AM
#5
Re: Quick Question
It has no choice but to work since the intellisense suggested it. Thanks for correcting me.
-
Dec 12th, 2006, 06:05 PM
#6
Re: Quick Question
 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.
-
Dec 12th, 2006, 06:23 PM
#7
Re: Quick Question
 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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|