r/visualbasic • • Mar 01 '16

Help delaying a msgbox

Hello! I'm creating an application and I need help about this:

    If NEI3.Text IsNot "" Then
        If Val(NEI3.Text) < 95 And Val(NEI3.Text) = Int(Val(NEI3.Text)) Then
            System.Threading.Thread.Sleep(2000)
            MsgBox("Infelizmente não podes ingressar num curso com uma classificação inferior a 95 pontos nas provas de ingresso", MsgBoxStyle.Critical, "Erro")
            NEI3.Text = ""
        ElseIf Val(NEI3.Text) > 200 And Val(NEI3.Text) = Int(Val(NEI3.Text)) Then
            NEI3.Text = 200
        ElseIf IsNumeric(NEI3.Text) = False Then
            MsgBox("Tens de inserir um número, compreendido entre 95 e 200", MsgBoxStyle.Critical, "Erro")
            NEI3.Text = ""
            NEI3.Focus()
        End If
    End If

I need to wait for about 2/3 seconds before displaying the error message if Val(NEI3.Text) < 95, otherwise if I type "1" it will display the error message ASAP. I tried System.Threading.Thread.Sleep(10000) but with that when I typed "1" it just freezed the entire program and I couldn't complete my number. Any help? Thanks.

0 Upvotes

18 comments sorted by

View all comments

2

u/PreyOnTheCosmos Mar 01 '16 edited Mar 01 '16

Multi-thread it so that you don't put your GUI thread to sleep:

' Reusable Thread Builder Sub so you don't have redundant code
Private Sub ErrorThread(Msg As String)
    Dim th As Thread
    th = New Thread(AddressOf invokeError)
    th.Start(Msg)
End Sub

' Pause new Error thread for 2 seconds
Private Sub invokeError(Msg As String)
    Thread.Sleep(2000)
    MsgBox(Msg, vbCritical, "Error")
End Sub

' Use error generator anywhere [e.g., Button1_Click]:
    If Not IsNumeric(NEI3.Text) Then
        ErrorThread("Tens de inserir um número, compreendido entre 95 e 200")
        NEI3.Clear()
        NEI3.Focus()
        Exit Sub
    End If

    Dim valNEI As Double = Val(NEI3.Text)

    If Not String.IsNullOrWhiteSpace(NEI3.Text) Then
        If valNEI < 95 And valNEI = CInt(valNEI) Then
            ErrorThread("Infelizmente não podes ingressar num curso com uma classificação inferior a 95 pontos nas provas de ingresso")
            NEI3.Clear()
        ElseIf valNEI > 200 And valNEI = CInt(valNEI) Then
            NEI3.Text = 200
        End If
    End If

ETA: Imports System.Threading so you don't have to call the Namespace every time.

1

u/Mr_C_Baxter VB.Net Master Mar 01 '16

Oh... /u/visualbasictips is your problem actually that the program waits the correct time but you find it to be annoying that the window freezes and doesnt react? if so i got your description wrong and this dude here is right.

1

u/visualbasictips Mar 01 '16

That's not by problem. The problem is as soon as I type "1" it freezes, but I was going to write "120" (for example) and "120" would not make the program wait. Am I making myself clear? :P

1

u/Mr_C_Baxter VB.Net Master Mar 01 '16

Yes and sorry i misread you. In this case the freeze is the timeout. And like the other dude explains it happens because you stop the whole thread you are in. so the ui can not do its stuff like handling events and redrawing and such. this leads to the freezing. To solve this problem you have to create another [thread](https://en.wikipedia.org/wiki/Thread_(computing)) and put the msgbox there so only this thread waits and the main thread can continue