9 Replies - 549 Views - Last Post: 23 February 2013 - 06:14 AM Rate Topic: -----

#1 cirustus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-February 13

array with timer

Posted 21 February 2013 - 03:51 AM

hi guys im stuck on how to do a timer on my array so that it displays each full name in my list box every 2 seconds and have the progress bar step each time


Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click
        ProgressBar1.Maximum = 100
        ProgressBar1.Step = 20
        Dim names(4, 1) As String
        names(0, 0) = "john"
        names(0, 1) = "dole"
        names(1, 0) = "Kylie"
        names(1, 1) = "higgins"
        names(2, 0) = "jade"
        names(2, 1) = "tugwell"
        names(3, 0) = "michael"
        names(3, 1) = "straps"
        names(4, 0) = "cyndi"
        names(4, 1) = "beamon"

        lstBox1.Items.Clear()
        For i As Integer = 0 To 4
            lstBox1.Items.Add(names(i, 0) + " " + names(i, 1))
            ProgressBar1.PerformStep()
            

        Next

    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: array with timer

#2 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: array with timer

Posted 21 February 2013 - 06:44 AM

Hi cirustus,
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
        Threading.Thread.Sleep(1000)


Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: array with timer

Posted 21 February 2013 - 06:50 AM

This blocks the current thread it is placed on, and this case it'll be the GUI thread. Thus locking up / freezing the application.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: array with timer

Posted 21 February 2013 - 07:09 AM

View PostAdamSpeight2008, on 21 February 2013 - 08:50 AM, said:

This blocks the current thread it is placed on, and this case it'll be the GUI thread. Thus locking up / freezing the application.



Yes AdamSpeight2008 is correct the Threading.Thread.Sleep() will freeze the thread that your program is running on and will cause the items to not be added to the listbox every 2 seconds so, I would try to use a timer to do it in the timer tick event. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: array with timer

Posted 21 February 2013 - 07:13 AM

It'll still add the items every two seconds, but'll freeze the application during those two seconds.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: array with timer

Posted 21 February 2013 - 08:15 AM

It will add the items but, they do not show in the textbox until the for next loop is done. I guess you could use
ListBox1.Update()

before the the sleep() command to update the listbox on each step. However, it does freeze the thread your program is running on so the user can`t do anything with the program until the for next loop is finished. A timer would not freeze it but may be a little more complex to set up.
Was This Post Helpful? 0
  • +
  • -

#7 dbasnett  Icon User is offline

  • D.I.C Addict

Reputation: 108
  • View blog
  • Posts: 601
  • Joined: 01-October 08

Re: array with timer

Posted 21 February 2013 - 08:51 AM

You could use a BackgroundWorker and have the .DoWork .ReportProgress every two seconds. The .ReportProgress event handler could update the listbox.

http://msdn.microsof...v=vs.90%29.aspx
Was This Post Helpful? 0
  • +
  • -

#8 cirustus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-February 13

Re: array with timer

Posted 21 February 2013 - 11:09 PM

View PostIronRazer, on 21 February 2013 - 06:44 AM, said:

Hi cirustus,
You could use a sleep command inside the for i loop like this. The 2000 is the milliseconds that it sleeps for which would be 2 seconds. The other way is to use a timer and have the timer tick event add the names and step the progress bar.
        Threading.Thread.Sleep(1000)



thankyou very much this help lots as well as
listbox1.update()

im now getting the names to list 1 after the other just gotta try work out how to make the progress bar step as the first name is displayed as it is currently stepping on the second name
Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict

Reputation: 220
  • View blog
  • Posts: 734
  • Joined: 01-February 13

Re: array with timer

Posted 22 February 2013 - 04:47 PM

You might need to change the place where you put the 2 lines. If you are still using the same for loop from your first post then try this :
        For i As Integer = 0 To 4

            lstBox1.Items.Add(names(i, 0) + " " + names(i, 1))
            ProgressBar1.PerformStep()

            'Update the listbox here
            'Put the sleep command here
        Next


This should do the following in this order - add the names to the listbox , step the progressbar, update the listbox, and then sleep 2 seconds. When i tested it it stepped the progressbar on the first name.

This post has been edited by IronRazer: 22 February 2013 - 04:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: array with timer

Posted 23 February 2013 - 06:14 AM

get rid of the loop, let the timer handle the iteration by incrementing i, adding to the listbox, and incrementing the progressbar.

To do this you will need to declare i and names() is a wider scope so that they're accessible in the timer routine.

Public Class Form1
    'declared at class level which makes them available throughout all routines in the class
    Dim i As Integer = 0
    Dim names(4, 1) As String

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'set up your timings
        ProgressBar1.Maximum = 100
        ProgressBar1.Step = 20
        Timer1.Interval = 2000 'This is approx 2 seconds

        names(0, 0) = "john"
        names(0, 1) = "dole"
        names(1, 0) = "Kylie"
        names(1, 1) = "higgins"
        names(2, 0) = "jade"
        names(2, 1) = "tugwell"
        names(3, 0) = "michael"
        names(3, 1) = "straps"
        names(4, 0) = "cyndi"
        names(4, 1) = "beamon"

        'reset pb and lb in case we it the button again.
        progressbar1.value =0
        lstBox1.Items.Clear()
        'Everything's set up, the listbox is clearStart the timer 
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        'With each tick of the timer
        lstBox1.Items.Add(names(i, 0) & " " & names(i, 1))
        ProgressBar1.PerformStep()
        'Now that names(i,0) & " " & names(i,1) has been added, we need to increment i for the next tick
        i += 1
        'if we are over 4 we stop the timer as we are done. Of course, you will want to make these variables that 
        'coincide with the bounds of your array so that if you add a names(5,0) it will appear.
        If i > 4 Then Timer1.Stop()
    End Sub
End Class

This post has been edited by CharlieMay: 23 February 2013 - 06:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1