为什么我的表单在使用从文本文件中读取的数组时会冻结?

时间:2017-11-08 19:48:43

标签: .net arrays vb.net filereader

所有语法都是正确的,当我运行代码时没有出现错误,但是当我单击表单上的登录按钮时,它只是冻结,我认为这是由于数组。 这是下面的代码。我不知道如何使其高效使其不冻结。

Imports System.IO
Public Class Form3

    Dim reader1 As System.IO.StreamReader
    Dim classdata As Array
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        reader1 = My.Computer.FileSystem.OpenTextFileReader("C:\Users\ryanm_000\Documents\username and password.txt")
        While reader1.EndOfStream = True
            classdata = reader1.ReadLine.Split(",")
            If TextBox1.Text = "admin" AndAlso TextBox2.Text = "iamtheboss" Then
                MsgBox("Welcome Mr Khan", MsgBoxStyle.Information, "Admin")
                Me.Hide()
                My.Forms.Form7.Text = Now.ToString
                My.Forms.Form7.Show()

            ElseIf TextBox1.Text = classdata(0) AndAlso TextBox2.Text = classdata(1) Then
                Me.Hide()
                My.Forms.Form5.Text = Now.ToString
                My.Forms.Form5.Show()
                reader1.Close()
            Else
                MsgBox("Sorry, username or password is invalid, please try again", MsgBoxStyle.Information, "Error")

            End If
        End While
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

您希望在阅读器不在流的末尾时处理文件。 尝试将您的while条件更改为While reader1.EndofStream = False或(更好)您可以写While Not reader1.EndOfStream