Question Loop and listBox?

thek

Member
Joined
May 31, 2009
Messages
22
Programming Experience
Beginner
Hi guys
I have a problem with loop and listBox.

I would like to test if ListBox1 is empty then test again until ListBox1 is NOT empty
If ListBox1 is NOT empty then msgBox “You did it”
and start over again, until i click a stop button

The problem is that when I start my program the listBox is ALWAYS empty and after 1-4 sec data will arrive in listBox1, but my loop (for testing if listBox1 is empty) is freezing up the application, because it’s always true and just looping infinity

Any hints or help?
 
Hi

Thanks for your quick reply.

Well the problem is that something listbox1 is empty, but then i have to wait 3 sec and a new item pops up, so if I set the listbox.items.count = 0 then the condition is met (at the moment), but i don't want it to end, becuase in 4 sec a new item pops up and so on.
 
Good point :eek:

Create a form
3 buttons
3 listbox'es (lstSource, lstTarget, lstUse)
In lstSource you can add 10 lines (just random text)


Here is the code:

VB.NET:
Public Class Form1
    Public time As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TransferData()

    End Sub
    Private Sub TransferData()
        ' Grabs the first line in lstSource
        Dim line As String
        line = lstSoruce.Items.Item(0)

        ' Gets a number between 1 and 3 and sets it to Timer1
        Dim rand As New Random
        time = rand.Next(1, 1)
        Timer1.Enabled = True

        ' Closes because error accurs when lstSource is empty
        If lstSoruce.Items.Count = 1 Then
            Timer1.Enabled = False
            MessageBox.Show("Closing program")
            Close()
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' Grabs the first line in lstSource
        Dim line As String
        line = lstSoruce.Items.Item(0)
        time -= 1
        If time = 0 Then
            'When "times up", we add the current line from lstSource in lstTarget
            lstTarget.Items.Add(line)
            ' Then delete the current line
            lstSoruce.Items.Remove(lstSoruce.Items.Item(0))
        End If
        ' Starts over
        TransferData()


    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ' Just to stop the transfer
        Timer1.Enabled = False
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click


        ' Here is the problem. This works fine, but only when I click Button3, I want it to run all the time and just "wait" if there is no items in lstTarget
        If lstTarget.Items.Count = 0 Then
            ' do nothing
        Else
            Try
                ' Grabs the first line in lstTarget
                Dim read_line As String
                read_line = lstTarget.Items.Item(0)
                ' Add the line i lstUse
                lstUse.Items.Add(lstTarget.Items.Item(0))
                ' Delete the current line
                lstTarget.Items.Remove(lstTarget.Items.Item(0))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
End Class
 
thek said:
' Here is the problem. This works fine, but only when I click Button3, I want it to run all the time and just "wait" if there is no items in lstTarget
If lstTarget.Items.Count = 0 Then
' do nothing
Then reverse your logic and only do something is the count does not equal 0
VB.NET:
If lstTarget.Items.Count <> 0 Then
            ' do something
 
Hi Hack

Thanks, but it gives me the same problem.

VB.NET:
start:

        If lstTarget.Items.Count <> 0 Then
            Dim read_line As String
            read_line = lstTarget.Items.Item(0)
            lstUse.Items.Add(lstTarget.Items.Item(0))
            lstTarget.Items.Remove(lstTarget.Items.Item(0))
        Else
            System.Threading.Thread.Sleep(500)
            GoTo start
        End If

As soon as the lstbox is empty it stall's :(

I don't get it. Why is this so hard? I just want a loop to test if there is data in a lstBox,
if yes = process it.
if no = wait 0.5 sec and try to test again.
It should only stop, when I press a button.

It sounds so easy in my head, but vb.net does not agree with me :D
 
if yes = process it.
if no = wait 0.5 sec and try to test again.
This is what it does.
Your only "problem" is the thread.sleep. Do you know what it means? Roughly translated it means: Take away all computing power from this thread for 500 ms and then continue. Since the thread that sleeps is your "main" or "UI" thread, your application becomes unresponsive or "stalls".

Personally I don't see any reason why a timer or endless loop should be used to check the content of a listbox. YOUR code adds items and removes items. So these functions are the place where you would do your checking.
 
@jmcilhinney: Sorry and thanks.

@picoflop
No I wasn't aware of that (application becomes unresponsive or "stalls"), but I still have the same problem, after removing the Sleep line. As soon as lstTarget is empty it stalls, I don’t want that. I want it to wait and see, because in 2 sec new data comes into lstTarget and the process should start over again (and again and again…)

It only runs correct, when I press Butten3 and it run once, but I want it to run all the time. So when I click button3 it then checks for data, waits until new data arrives, then checks for data, waits for new.... and so on.
Then I click e.g. button4, it should stop.

Thanks for your clarification on the sleep function.

Maybe I do not make myself so clear (and sorry about that ;-) but I hope you can help me.

If needed I can give a more detailed explanation on my “challenge”.
 
If your remove thread.sleep your app simply stalls because you have a "tight loop".
Just try a do-loop without any code. It will stall your app too. Simply because there is no "time" do do anything else than just to process the loop.

because in 2 sec new data comes into lstTarget
Your data does not "come". You have a function or piece of code where you put the data into the listbox. That's the point where your checking function might be triggered. Also there is a ListBox.Remove(item) somewhere in your code - another point to check if the listbox has more items or not.
 
That's the point where your checking function might be triggered.

Also there is a ListBox.Remove(item) somewhere in your code - another point to check if the listbox has more items or not.

Good points, I'll try - otherwise I'll let you know :)
 
Back
Top