|
-
Jul 2nd, 2004, 08:38 AM
#1
Thread Starter
Registered User
run this code 1000 milliseconds from now...
whats the simplest way to do this in the middle of your code:
in 1000 milliseconds do this:
....
until then, continue normally with the rest of the code.
?
i can use a timer, and make it destray itself after it executes the first time, but that seems silly. is there a better way?
-
Jul 2nd, 2004, 08:59 AM
#2
Fanatic Member
I am not familiar with the subject of threads but you could have a thread which checks the time every 100 ms or so then do what you want when you hit x.
-
Jul 2nd, 2004, 10:06 AM
#3
PowerPoster
Re: run this code 1000 milliseconds from now...
Originally posted by marvinklein
whats the simplest way to do this in the middle of your code:
in 1000 milliseconds do this:
....
until then, continue normally with the rest of the code.
?
i can use a timer, and make it destray itself after it executes the first time, but that seems silly. is there a better way?
I don't see the point of such a small delay, even in a game let alone a commercial application.
It depends on the code you want to keep running. Could you not enclose the code you want to run within a loop. Immediately before the loop is entered
Dim dTime as datetime = Now
Inside the loop
If Now>dTime.AddMilliseconds(1000) Then
(Code to do this)
End If
If you want to continue with the same code inside the loop without the delay, use a boolean to avoid this segment.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 2nd, 2004, 10:07 AM
#4
Hyperactive Member
Yep, this should work just fine for you
VB Code:
Dim tEnd As DateTime = Now
While Now < tEnd.AddMilliseconds(1000)
Trace.Write("Still less than 1 second" & vbCrLf)
End While
Trace.Write("OK, it's over" & vbCrLf)
Whadayamean it doesn't work....
It works fine on my machine!

-
Jul 2nd, 2004, 10:20 AM
#5
You could also use the Thread.Sleep command:
VB Code:
System.Threading.Thread.Sleep(1000)
-
Jul 2nd, 2004, 10:49 AM
#6
Hyperactive Member
But....sleeping the thread, loop works or is it stopped, too?
I never used 'sleep', can someone clear this point? Thanks!
Live long and prosper (Mr. Spock)
-
Jul 2nd, 2004, 11:06 AM
#7
PowerPoster
Originally posted by alextyx
But....sleeping the thread, loop works or is it stopped, too?
I never used 'sleep', can someone clear this point? Thanks!
Have a look at previous threads on waiting and sleep.
If you use sleep you may get unexpected results if you are not careful.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jul 2nd, 2004, 11:35 AM
#8
Hyperactive Member
I had sensation, just for what I read (but not applied), that sleep stoped execution of the thread code for a certain amount of time. Because we need to let a loop running, I'm asking if I well understood as sleep acts...that's all!
But because it's not so urgent, if you can't answer to my question, I think I will solve my doubt when it will be necessary, when I will need to use something like that, probably!
Live long and prosper (Mr. Spock)
-
Jul 2nd, 2004, 01:37 PM
#9
Thread Starter
Registered User
a while loop seems silly because it keeps checking constantly. what if i want to delay for a long time. hardly seems efficient. i guess i could use threading, but i think i will just stick to my original idea of using a timer.
let me know if you think there's something terribly wrong with that.
-
Jul 2nd, 2004, 03:21 PM
#10
Hyperactive Member
Dear Marvin, I am an estimator of timer, because I used polling routines fired by timing interrupt in industrial firmware, where the hardware to implement a completely external interrupt structure should be too much expensive, so I'm not the right man to give an opinion. I have to admit timers drain some resources and have some unexpected behaviour. I think a timer is the cause of a strange behaviour I discussed in another thread, but I have to verify that.
In general, if you have to build another thread (resources immediatly drained!) that has to check if time is elapsed (How? perhaps with a loop, or...... with a Timer) it could be a good idea using a timer directly. You have to remember that timer works, also if another form is showed in modal way (the line below Frmxxx.showdialog is not executed until Frmxxx is closed, but timer ticks anyway) and.....the rest, as i said, has to be verified
So, if I were you, I used a Timer, but I'm on high level language's world from about a year ago, so I'm quite a newbie and I hope more qualified opinions will help you to decide. Good job
Live long and prosper (Mr. Spock)
-
Jul 3rd, 2004, 01:38 PM
#11
Thread Starter
Registered User
i just created a new thread, the first line of which is thread.sleep(1000)...
i think that's best..
-
Jul 3rd, 2004, 03:46 PM
#12
PowerPoster
Originally posted by marvinklein
i just created a new thread, the first line of which is thread.sleep(1000)...
i think that's best..
What is the point of that? You normally want a pause to enable something to remain on view for a short period to automatically give the operator time to react or absorb information. You CAN use Sleep for this but as it suspends everything you must update visible objects before the Sleep is invoked.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
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
|