|
-
Jul 6th, 2010, 02:29 PM
#1
Thread Starter
Fanatic Member
My Wait Function
If you dont want to use Threading.Thread.Sleep then you may want to use this.
VB.NET Code:
Private Sub Wait(ByVal Milliseconds As Integer) Dim sw As New Stopwatch sw.Start() Do While sw.ElapsedMilliseconds < Milliseconds Application.DoEvents() Loop sw.Stop() End Sub
Last edited by Emcrank; Jul 8th, 2010 at 04:23 PM.
-
Jul 6th, 2010, 03:04 PM
#2
Re: My Wait Function
 Originally Posted by Emcrank
If you dont want to use Threading.Thread.Sleep then you may want to use this.
VB.NET Code:
Private Sub Wait(ByVal Seconds As Integer)
Dim WaitTimer As New Timer
AddHandler WaitTimer.Tick, AddressOf WaitTimerTick
WaitTimer.Interval = Seconds * 1000
WaitTimer.Start()
End Sub
Private Sub WaitTimerTick(ByVal sender As Object, ByVal e As System.EventArgs)
'CODE HERE
DirectCast(sender, Timer).Stop()
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?
-
Jul 6th, 2010, 04:08 PM
#3
Thread Starter
Fanatic Member
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
-
Jul 7th, 2010, 03:50 AM
#4
Re: My Wait Function
 Originally Posted by Emcrank
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
-
Jul 7th, 2010, 08:08 AM
#5
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.
-
Jul 7th, 2010, 12:41 PM
#6
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
-
Jul 8th, 2010, 05:12 AM
#7
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...
-
Jul 8th, 2010, 11:20 AM
#8
Thread Starter
Fanatic Member
Re: My Wait Function
suppose your right, get a mod to delete it if you want
-
Jul 8th, 2010, 04:24 PM
#9
Thread Starter
Fanatic Member
Re: My Wait Function
Brand new wait function. Works like a charm. No need for this to be deleted now
Last edited by Emcrank; Jul 8th, 2010 at 04:46 PM.
-
Jul 8th, 2010, 04:57 PM
#10
Re: My Wait Function
 Originally Posted by Emcrank
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
-
Jul 8th, 2010, 05:07 PM
#11
Thread Starter
Fanatic Member
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
-
Jul 8th, 2010, 05:30 PM
#12
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.
-
Jul 8th, 2010, 06:11 PM
#13
Thread Starter
Fanatic Member
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.
-
Jul 9th, 2010, 05:01 AM
#14
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:
Private Sub Wait(ByVal Milliseconds As Integer) Dim sw As New Stopwatch sw.Start() Do While sw.ElapsedMilliseconds < Milliseconds Application.DoEvents() Threading.Thread.Sleep(100) Loop sw.Stop() 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% 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.
-
Jul 9th, 2010, 06:52 AM
#15
Thread Starter
Fanatic Member
Re: My Wait Function
OK ill keep that in mind for if i need to wait longer than 100 ms. Thankyou.
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
|