开发者

How to stop and starts a running loop of number on a text file or database in visual basic

I need a small app that randomly shuffles a set of preloaded numbers. The shuffling will be visible on the screen and goes on continuously (looping) until a key is pressed and as soon as that happens it will show the winning number. Am using visual basics 2008 to develop the application but my problem is when i start the app and hit a key it will loop through my numbers on the text file and it will also display the event but if i hit a key to stop it will not stop...Need help please

I was workin on the app but my problem is how to stop the loop and resume on the with a keypress. below is the app codes.

Dim Running As Boolean = False

Sub ReadAccounts()

    Dim arr As New ArrayList
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-11111")
    arr.Add("1366-665885-666-22650")
    arr.Add("1366-665885-666-22650")

    Try
        Dim 开发者_JS百科rnd As New Random

        For i As Integer = 0 To arr.Count - 1

            Dim Disarray As String = arr(i).ToString()
            System.Threading.Thread.Sleep(100) ' set sleep time
            lblAccounts.Text = Disarray

            lblAccounts.Update()
            Application.DoEvents()

        Next

        If Running Then
            Running = False
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
    ' e.Handled = True
End Sub

Private Sub Form2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    If System.Char.IsWhiteSpace(e.KeyChar) = True Then

        If Running Then
            Running = False
        Else
            Running = True
            ReadAccounts()
        End If

    End If

End Sub


First I would load the numbers from the file into an array or into a List, instead of continually reading from the file inside a loop.

Then it dependes on the type of project you have. If it is a console app, I would test if a key press is available at each loop:

If Console.KeyAvailable Then
    'terminate your loop
End If

I also would include a System.Threading.Thread.Sleep(<time in ms>) in the loop in order to control the display frequency of the numbers. Otherwise your PC will become unresponsive.

If it is a WinForms app, then you could use a System.Windows.Forms.Timer. You can place it on the form and configure it in the properties window. Add an event handler for the Tick-event of the timer (much like you would add a click event handler for a button) where you will place the code, that displays the next random number. Will not need a loop at all. This way your app will always stay responsive.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜