Results 1 to 12 of 12

Thread: run this code 1000 milliseconds from now...

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283

    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?

  2. #2
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018
    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.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    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.

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Yep, this should work just fine for you
    VB Code:
    1. Dim tEnd As DateTime = Now
    2.         While Now < tEnd.AddMilliseconds(1000)
    3.             Trace.Write("Still less than 1 second" & vbCrLf)
    4.         End While
    5.         Trace.Write("OK, it's over" & vbCrLf)
    Whadayamean it doesn't work....
    It works fine on my machine!

  5. #5
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    You could also use the Thread.Sleep command:

    VB Code:
    1. System.Threading.Thread.Sleep(1000)

  6. #6
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  8. #8
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  9. #9

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    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.

  10. #10
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    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)

  11. #11

    Thread Starter
    Registered User
    Join Date
    Jul 2001
    Posts
    283
    i just created a new thread, the first line of which is thread.sleep(1000)...

    i think that's best..

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
  •  



Click Here to Expand Forum to Full Width