Results 1 to 17 of 17

Thread: Delay

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2004
    Location
    Poole UK
    Posts
    1

    Question Delay

    how can I insert a small (1 sec ) delay between 2 numbers being displayed in 2 textbox's.
    i.e a number is shown in textbox1 then 1 second later a second number is shown in textbox2
    I have the numbers appearing in the right boxes but instantly

    p.s
    I am a begginer at VB

    Thanks Trevor

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

    Put the following immediately after the code which reveals the first number.

    VB Code:
    1. Dim Present as datetime=now
    2. do
    3. if now>present.addmilliseconds(1000) then exit do
    4. Loop
    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.

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    VB Code:
    1. System.Threading.Thread.Sleep(1000)

    Try searching the forum first before you post - this question has been asked many times.

    Mike

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Mike Hildner
    VB Code:
    1. System.Threading.Thread.Sleep(1000)

    Mike
    If you use Sleep then you have to update your object BEFORE it in the code stream.

    E.G.

    VB Code:
    1. TextBox1.Text ="1"
    2. TextBox1.Update  (  or Me.Update if necessary)
    3. System.Threading.Thread.Sleep(3000)   (Halts thread for 3
    4.                                                                 seconds)
    5. TextBox2.Text ="2"


    Edited: Sleep(1000) to Sleep(3000) after prompting.
    Last edited by taxes; Jul 17th, 2004 at 06:14 PM.
    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.

  5. #5
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Yes you do. But it's still less typing , especially when you Import.

  6. #6
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Originally posted by taxes
    If you use Sleep then you have to update your object BEFORE it in the code stream.

    E.G.

    VB Code:
    1. TextBox1.Text ="1"
    2. TextBox1.Update  (  or Me.Update if necessary)
    3. System.Threading.Thread.Sleep(1000)   (Halts thread for 3
    4.                                                                 seconds)
    5. TextBox2.Text ="2"
    Halts thread for 3 seconds? It halts for 1
    \m/\m/

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

    "Halts thread for 3 seconds? It halts for 1"

    Sorry, my mistype. It shouldn't have taken him long to figure that out though
    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
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Mike Hildner
    Yes you do. But it's still less typing , especially when you Import.
    But don't forget Sleep stops the entire thread so if other things are going on they are aslo delayed.
    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.

  9. #9
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    why not just throw a for next loop between the lines of code that insert the values?

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Andy
    why not just throw a for next loop between the lines of code that insert the values?
    Do you REALLY mean that
    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.

  11. #11
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    I know there are two easy ways to work with asyncronous events without multithreading. Timers and Application Idle.
    Taxes's first solution seems very good if you avoid the heavy looping, so your app. can react for button clicks and so on. I have never applied it, only read something, but I think you can test system clock, as Taxes suggested, in Application Idle event. Repeat...I've never tried this way. It should be possible and convenient, but you have to verify directly. Good job!
    Live long and prosper (Mr. Spock)

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

    do you mean something like

    VB Code:
    1. Dim Present as datetime=now
    2. do
    3. Application.DoEvents
    4. if now>present.addmilliseconds(1000) then exit do
    5. Loop

    This will allow events occuring outside of the loop to be recognised, although it is hardly necessary in the case of a 1 second delay (except perhaps in a game.)
    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.

  13. #13
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Hi taxes. Your is a anyway a solution to process events like button clicks (that was my example!). Probably, at this level, yours is the best solution. I have thought that, but I wanted to propose something more. Problem is I'm speaking about something I've only read on a book and never applied. If I undestood well, there is an event (application idle) that occurs every not fixed intervals, but surely several times par second and you can use it to perform a test like yours, but as I said, your first solution, integrated with a simple Application.Doevents, seems very good!
    And....I've not experimented if Application.Idle occurs also if a classic loop is processing. Timer can fail in this situation, but I have not found detail about what could happen on Application.Idle event. It occurs anyway, or not? I will test it....in the future!
    Live long and prosper (Mr. Spock)

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

    Not quite sure what you mean. Thr Application.Idle event is fired each time the application is about to go into the idle state and you use it to contain code which must be accomplished before that happens. (Update of controls is an example). As far as I know, it is not fired "several times a second".
    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.

  15. #15
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Yes Taxes, it's possible you were right. As I said, I haven't tried it. In the example I found, the idle event was used to update time in the title of form (very close to our problem). It suggests that idle fires frequently (is specified, anyway, that are not fixed intervals for this), but it's not clear what happens when the thread of the form is working heavily. It's possible it stops. A Timer will work, but if you are in a loop, you need an application.doevents to permit it to fire. Probably you are asking why I'm worrying about this when you already gave solution. The reason is I'm thinking about my situation. I've a very very slow Notebook and clicking some buttons that fill listview from database, changing colors of subitem, calculating results, etc.. I put my PC in a task that requests many seconds. So I was thinking that could be better, for situation like this, to reverse our point of view: Let the form's code work normally and perform your code in an event routine (I thought Idle or Timer, probably only timer is good). So if you continue to work, also in a loop, like filling my listbox, you can also let your delay works and put your result in textbox after the specified time is elapsed. I think, one second is not a problem. We can use also a Sleep for thread, effectively, but I like your approach because it could be performed without stop the thread or drain all its resources. So, if tomorrow the delay will increase, we will not be in a situation of freezed code. Now a doubt.....was my english sufficiently good or none has understood anything?
    Live long and prosper (Mr. Spock)

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

    Yes your English is understood.

    It looks like you just have to experiment with your LapTop. Best of luck.
    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.

  17. #17
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    Thanks Taxes
    You are perfectly right on the idle event. I write the example application (by memory, but it's very short and easy) and I verified, as you said, it doesn't work properly. I can't imagine why it was proposed!
    Timer works, as I knew. Anyway we are started from an easy and already solved question to a far and complicated discussion.
    This is my test form's code. Form contains a Timer (300ms) and two buttons . As you can see, I use timer to refresh time, but you can easily adapt it to other use.

    VB Code:
    1. Dim flagstopciclo As Boolean = False
    2.  
    3.   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         'AddHandler Application.Idle, AddressOf Me.Orologio
    6.         Me.Timer1.Enabled = True
    7.  
    8.     End Sub
    9.  
    10.     Private Sub Orologio(ByVal sender As Object, ByVal e As System.EventArgs)
    11.  
    12.         Me.Text = Now
    13.  
    14.     End Sub
    15.  
    16.     Private Sub BtnFine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFine.Click
    17.  
    18.         Me.flagstopciclo = True
    19.  
    20.     End Sub
    21.  
    22.     Private Sub BtnParti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnParti.Click
    23.  
    24.         Do While Not Me.flagstopciclo
    25.             Application.DoEvents()
    26.         Loop
    27.  
    28.         MessageBox.Show("Finito")
    29.         Me.flagstopciclo = False
    30.  
    31.     End Sub
    32.  
    33.     Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    34.  
    35.         Me.Orologio(Nothing, Nothing)
    36.  
    37.     End Sub

    P.S. I need not only to experiment ...I need to buy a new Notebook!!! (550Mhz 256Kram 12,1"LCD not TFT)
    Live long and prosper (Mr. Spock)

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