User7291
User7291

Reputation: 1115

Replacing my function with a better one

i'm trying to popup a msgbox everytime the current time is equal to a field in a table in the database i'm using the following code:

For i As Integer = 0 To contentTable.Rows.Count() - 1
    UpdateListBoxRec2("Sending content " & i + 1 & " to :") 
    For j As Integer = 0 To subTable.Rows.Count() - 1
        While send = False
            If contentTable.Rows(i).Item(1) = DateTime.Now.ToString("hh:mm") Then
                UpdateListBoxRec2(subTable.Rows(j).Item(0))
                MsgBox(contentTable.Rows(i).Item(0))
                send = True
            End If
        End While
        'send = False
    Next
Next

this method works but when the debugger is in the while loop ( means when the current time is not yet equal to the time i have in my db table ) i can't use any tool of my windows app untill the debugger gets out of the while loop so i'm searching for a similar method to popup my msgbox in the same condition and to be able to use my tools in the app all the time

please note that i'm using vb.net

any help please :)

Upvotes: 1

Views: 57

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

You should never sit in an endless loop, like that, on the UI thread. As long as the UI thread is busy, it will be blocked from processing any incoming window messages (e.g. key strokes, mouse clicks). You could sit in a loop like that if you were on a separate thread, but that would be silly. The more sane solution would be to use a timer. For instance, if you drop a Timer component onto your form in the designer, and set it's Interval property to 5000 (five seconds), you can then add the following code:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    For i ...
        For j ...
            If contentTable.Rows(i).Item(1) = DateTime.Now.ToString("hh:mm") Then
                UpdateListBoxRec2(subTable.Rows(j).Item(0))
                MessageBox.Show(contentTable.Rows(i).Item(0))
            End If
        Next
    Next
End Sub

Now it will just run that code once every five seconds rather than continuously checking the current time and thereby hogging the UI thread.

Upvotes: 2

Related Questions