שתף באמצעות


VB.Net Display MsgBox but continue application

Question

Friday, April 13, 2012 12:24 AM

I have this For Next Loop with a Delay that runs but it stops running till I hit OK on the MsgBox.  what is the best way to do a task x times with x delay but not wait for MsgBox.   

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim var As Integer
        Dim startVal As Integer
        Dim endVal As Integer
        startVal = 1
        endVal = 5
        For var = startVal To endVal
            System.Threading.Thread.Sleep(2000)
            MsgBox("Message Box Shows " & var & " Times ")
        Next var
    End Sub

All replies (11)

Friday, April 13, 2012 6:49 PM ✅Answered

Fixed!  great article

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        Dim var As Integer        Dim startVal As Integer        Dim endVal As Integer        startVal = 1        endVal = 5        For var = startVal To endVal            ' MsgBox("Message Box Shows " & var & " Times ")            TextBoxRepeat.Text = ("Message Box Shows " & var & " Times ")            TextBoxRepeat.Refresh()            System.Threading.Thread.Sleep(2000)        Next var    End Sub

Friday, April 13, 2012 12:50 AM | 1 vote

Put it on a new thread:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim t As New System.Threading.Thread(AddressOf ShowMSG)
    t.Start()
End Sub

Private Sub ShowMSG()
    MessageBox.Show("hello")
End Sub

Cheers :)
~Ace

If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum


Friday, April 13, 2012 1:26 AM

Sorry,  unsure how to keep my Loop and Delay with the above code.  Fyi, I tried your code by itself and ever time you click button it does open a new message box.  but still need help with my run 5 times with 2 sec delay.  thanks  AceInfinity


Friday, April 13, 2012 1:33 AM

The code like you have, Message box will stop exectuting of the code. Its like System.Sleep() method, and will continue after pressing some command (ok, no, cancel, or which ever button.

So not even creating a new Thread, and putting the loop onto it, will not help. Displaying a message box, stops execution of the code  - until you dont press any button.

Mitja


Friday, April 13, 2012 1:42 AM

Thank you, any suggestion to have a process happen but still notify the user that something happened.  ie this was sample code, but was going to fire off an email 5 times after 2 sec delay.  these are configurable via my.settings.repeat and delay.   so it could be x times and x delay.  thoughts?


Friday, April 13, 2012 1:47 AM

Yes. Use some control like label. Do not use Message box, if you wanna the code to run.

If you will show it anyway, I think it will not do any harm. Code simply stops and waits user to press a button to continue.

Mitja


Friday, April 13, 2012 1:58 AM

This code only dispays the last message is this waht your thinking?  and what did I do wrong?  thanks for the input much better idea then a bunch of MsgBoxes

 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim var As Integer
        Dim startVal As Integer
        Dim endVal As Integer
        startVal = 1
        endVal = 5
        For var = startVal To endVal
            ' MsgBox("Message Box Shows " & var & " Times ")
            TextBoxRepeat.Text = ("Message Box Shows " & var & " Times ")
            System.Threading.Thread.Sleep(2000)
        Next var
    End Sub

Friday, April 13, 2012 2:23 AM

If you want the message to show previous messages as well as the new message, then append to the existing text istead of overwriting it.  This assumes the Multiline property of your text box has been set to True.

            TextBoxRepeat.Text &= "Message Box Shows " & var & " Times " & vbNewLine


Friday, April 13, 2012 5:49 PM

That works,  but it displays when it's done all the loops not displaying each pass.  ie if I have it run 5 times and System.Threading.Thread.Sleep(300000)  the box would be blank for 25 Min.  any way to display each time it runs?   


Friday, February 17, 2017 11:50 AM

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim var As Integer
        Dim startVal As Integer
        Dim endVal As Integer
        startVal = 1
        endVal = 5
        For var = startVal To endVal
            System.Threading.Thread.Sleep(2000)
            Dim T As Threading.Thread = New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf ModelessMsgBox))
            T.Start(var)
        Next var
    End Sub

    Private Sub ModelessMsgBox(var As Integer)
        MsgBox("Message Box Shows " & var & " Times ")
    End Sub

Friday, February 17, 2017 12:28 PM

Yea we can see that some persons can do simple things difficult. 

This is a lot easier

Be aware that although it is build in, is a messagebox just a predefined form.

Difficult does not mean better. 

Success
Cor