herpez
Jun 7th, 2010, 07:49 PM
Hi. I have my code for make an app which reads and shows a random line from a txt and never repeats the line. When it reads all, it displays that all lines have been shown and then resets all and starts.
However, and i don't know why, sometimes (not always), it freezes.
Here is the code
Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
Dim TextFile() As String
Dim dir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim FILE_NAME As String = (dir + "\\fernandopessoa.txt")
Dim RandomNumber As Integer
Dim RandomClass As New Random()
Dim var As String
Dim TextStringold As String
Dim strLanguage As String
Dim frase, frase2, frase3 As String
strLanguage = System.Globalization.CultureInfo.CurrentCulture.ToString.ToUpper.Substring(0, 2)
If strLanguage = "EN" Then
frase = "All quotes have been shown"
frase2 = "Atention"
frase3 = "File fernandopessoa.txt missing"
Else
frase = "Todas as frases foram mostradas"
frase2 = "Atenção"
frase3 = "Ficheiro fernandopessoa.txt em falta"
End If
If File.Exists(FILE_NAME) Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim s As String = objReader.ReadToEnd
Dim numero As Integer
Dim comparacao As String
TextFile = s.Split(CChar(vbLf))
numero = (TextFile.Length - 1)
If i > (numero + 1) Then
i = 1
TextString = "a,"
Label2.Text = ""
MsgBox(frase, 0, frase2)
Else
RandomNumber = RandomClass.Next(0, CInt(numero + 1))
comparacao = "," & RandomNumber & ","
While (InStr(TextString, CStr(RandomNumber)) <> 0) 'iguais
RandomNumber = RandomClass.Next(0, CInt(numero))
End While
TextStringold = TextString
TextString = TextStringold & RandomNumber & ","
i += 1
var = TextFile(RandomNumber)
Label2.Text = var
End If
objReader.Close()
objReader.Dispose()
Else
MsgBox(frase3, 0, frase2)
End If
End Sub
I have spent about 4h trying to solve, i tried with a the file fernandopessoa.txt with just 4 lines and with 120. With 120 it crashes always and on about the 110th click. This way, i thought it could be a freeze with an infinite loop in the random. However, when i put just 4 lines, it sometimes also crashes, so i think Random it's not the problem.
Need help please
However, and i don't know why, sometimes (not always), it freezes.
Here is the code
Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox8.Click
Dim TextFile() As String
Dim dir As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim FILE_NAME As String = (dir + "\\fernandopessoa.txt")
Dim RandomNumber As Integer
Dim RandomClass As New Random()
Dim var As String
Dim TextStringold As String
Dim strLanguage As String
Dim frase, frase2, frase3 As String
strLanguage = System.Globalization.CultureInfo.CurrentCulture.ToString.ToUpper.Substring(0, 2)
If strLanguage = "EN" Then
frase = "All quotes have been shown"
frase2 = "Atention"
frase3 = "File fernandopessoa.txt missing"
Else
frase = "Todas as frases foram mostradas"
frase2 = "Atenção"
frase3 = "Ficheiro fernandopessoa.txt em falta"
End If
If File.Exists(FILE_NAME) Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Dim s As String = objReader.ReadToEnd
Dim numero As Integer
Dim comparacao As String
TextFile = s.Split(CChar(vbLf))
numero = (TextFile.Length - 1)
If i > (numero + 1) Then
i = 1
TextString = "a,"
Label2.Text = ""
MsgBox(frase, 0, frase2)
Else
RandomNumber = RandomClass.Next(0, CInt(numero + 1))
comparacao = "," & RandomNumber & ","
While (InStr(TextString, CStr(RandomNumber)) <> 0) 'iguais
RandomNumber = RandomClass.Next(0, CInt(numero))
End While
TextStringold = TextString
TextString = TextStringold & RandomNumber & ","
i += 1
var = TextFile(RandomNumber)
Label2.Text = var
End If
objReader.Close()
objReader.Dispose()
Else
MsgBox(frase3, 0, frase2)
End If
End Sub
I have spent about 4h trying to solve, i tried with a the file fernandopessoa.txt with just 4 lines and with 120. With 120 it crashes always and on about the 110th click. This way, i thought it could be a freeze with an infinite loop in the random. However, when i put just 4 lines, it sometimes also crashes, so i think Random it's not the problem.
Need help please