Automatically close custom form a specified amount of time after it is displayed

I have a vb.net program that displays a custom form thus:

frmCutGlassMessage.Show()

I want the form to be displayed for only a specified period of time - say 3 seconds - and then automatically close.  Is there a property on the form that I can use to do this or is there some other way I can accomplish this task?
T HoecherlDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi T Hoecherl;

Place code like this in the custom form class and also add a Timer component as well.
Public Class CutGlassMessage

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        '' This value is in milliseconds, 1000 milliseconds = 1 sec
        Timer1.Interval = 3000
        Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Me.Close()

    End Sub

End Class

Open in new window

0
T HoecherlDeveloperAuthor Commented:
Fernando,

I used your code exactly as you gave it to me, after adding a timer called Timer1 to the form.  The form displays and then simply remains there.  It doesn't ever close, so the Me.Close() event in the Timer1_Tick subroutine is not happening.  Any ideas?

T
0
Fernando SotoRetiredCommented:
Hi T;

When you created the form in questioned did you use, frmCutGlassMessage.Show() or frmCutGlassMessage.ShowDialog()? If you used Show then try creating a two Form test app that shows the issue then zip it and upload to the web where I can download and test on my system. Please post the link here.
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

T HoecherlDeveloperAuthor Commented:
When I created the two form test app, it worked.  The form stays up for 3 seconds and then closes.  When I debug and step through the lines of code one line at a time I notice that in the test app, no matter what code follows the frmCutGlassMsg.Show() command in the main form, after three seconds, the Timer1_Tick subroutine code is activated and the form closes.  But in my production app, the Timer1_Tick subroutine code is not activated until after all of the code following frmCutGlassMsg.Show() has been executed.  Then and only then program execution jumps to the Timer1_Tick subroutine and the form closes.  What could be causing that?

T
0
Fernando SotoRetiredCommented:
Hi T;

The issue you are experiencing is do to the Windows application that runs the UI is a single threaded message loop. The message loop receives all the events that the UI needs to update and all this is done after the user code is done, meaning after the button click event is completed which in your case is too late. Can you post the code that is executed after the frmCutGlassMsg.Show() to the end of that event so that I can guide you to the best possible solution.

Simple solution would be to use Application.DoEvents() which will break away from user code to handle Window events or a harder solution multithreading.
0
T HoecherlDeveloperAuthor Commented:
frmCutGlassMsg.Show()
        'Threading.Thread.Sleep(1500)
        'frmCutGlassMsg.Hide()

        For Each itemChecked In clbBatches.CheckedItems
            strBatch = itemChecked.item(0).ToString

            strFileNameGlass = strBatch.Substring(6, 5) & strBatch.Substring(13, 2) & "c.dat"
            strFileNameIntcpt = strBatch.Substring(6, 5) & strBatch.Substring(13, 2) & "i.dat"

            'UPDATE ASSORTED_BIN FIELD
            cmd.CommandText = "EXECUTE udp_ASSORTED_BIN_NUMBER '" & strBatch & "'"
            cmd.ExecuteNonQuery()

            'CREATE CUT GLASS FILE
            Dim column_1 As String
            Dim oWrite As System.IO.StreamWriter
            My.Computer.FileSystem.CreateDirectory("CutGlass")

            oWrite = System.IO.File.CreateText(strPathGlass & strFileNameGlass)

            Dim selectSQL As String = "EXECUTE udp_CUT_GLASS '" & strBatch & "'"

            Dim com As New SqlCommand(selectSQL, cn)
            com.CommandTimeout = 0
            com.CommandType = CommandType.Text

            Dim myreader As SqlDataReader = com.ExecuteReader

            While myreader.Read
                column_1 = myreader.GetString(0)
                oWrite.WriteLine(String.Format("{0}", column_1))
            End While
            oWrite.Close()
            myreader.Close()

            frmInterceptMsg.Show()
            'Threading.Thread.Sleep(1500)
            'frmInterceptMsg.Hide()

            'CREATE INTERCEPT FILE
            Dim column_1a As String
            Dim oWrite1a As System.IO.StreamWriter
            My.Computer.FileSystem.CreateDirectory("Intercept")

            oWrite1a = System.IO.File.CreateText(strPathIntercept & strFileNameIntcpt)

            Dim selectSQL1a As String = "EXECUTE udp_INTERCEPT_FILE '" & strBatch & "'"

            Dim com1a As New SqlCommand(selectSQL1a, cn)
            com1a.CommandTimeout = 0
            com1a.CommandType = CommandType.Text

            Dim myreader1a As SqlDataReader = com1a.ExecuteReader

            While myreader1a.Read
                column_1a = myreader1a.GetString(0)
                oWrite1a.WriteLine(String.Format("{0}", column_1a))
            End While
            oWrite1a.WriteLine(String.Format("{0}", "#"))
            oWrite1a.Close()
            myreader1a.Close()

            frmTemperedPOMsg.Show()
            'Threading.Thread.Sleep(1500)
            'frmTemperedPOMsg.Hide()

            'CREATE TEMPERED GLASS PO FILE
            Dim column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23 As String
            Dim oWrite2 As System.IO.StreamWriter

            Dim intCount As Integer = 0

            cmd.Connection = cn
            cmd.CommandTimeout = 0
            cmd.CommandType = CommandType.Text
            cmd.CommandText = "EXECUTE udp_TEMPERED_PO '" & strBatch & "'"
            Convert.ToInt32(cmd.ExecuteNonQuery())

            cmd.CommandText = "SELECT COUNT(*) AS BATCHCOUNT FROM PCT_TEMPERED_PO_FINAL"
            intCount = Convert.ToInt32(cmd.ExecuteScalar())


            If intCount > 0 Then
                Dim selectSQL2 As String = "SELECT Bin, Assorted_Bin, Item_Number, Glass,itemdesc, Order_Number, CustName, Line_Number, Batch_Number, status, WinPart, WinPos, xwpFixedUnitWidth, xwpFixedUnitHeight,xwpVentUnitWidth, " & _
                    "xwpVentUnitHeight,Quantity, xwpTempered,FUW_FRACT, FUH_FRACT, VUW_FRACT, VUH_FRACT,SEQUENCE FROM PCT_TEMPERED_PO_FINAL ORDER BY SEQUENCE"
                Dim t As Char = vbTab

                Dim com2 As New SqlCommand(selectSQL2, cn)
                com2.CommandType = CommandType.Text

                My.Computer.FileSystem.CreateDirectory("TemperedPO")

                oWrite2 = System.IO.File.CreateText(strPathPO & strBatch & ".txt")

                Dim myreader2 As SqlDataReader = com2.ExecuteReader

                Dim schemaTable As DataTable = myreader2.GetSchemaTable()
                Dim collist = From eachrow In schemaTable.Rows() Select eachrow(0)

                Dim strcolname As String = String.Join(vbTab, collist)
                oWrite2.WriteLine(strcolname)

                'For i = 0 To 22
                '    oWrite2.Write("{0]", CStr(myreader2.GetString(i)))
                'Next i
                'oWrite2.WriteLine()
                While myreader2.Read
                    column_1 = CStr(myreader2.GetString(0))
                    column_2 = CStr(myreader2.GetString(1))
                    column_3 = CStr(myreader2.GetString(2))
                    column_4 = CStr(myreader2.GetString(3))
                    column_5 = CStr(myreader2.GetString(4))
                    column_6 = CStr(myreader2.GetString(5))
                    column_7 = CStr(myreader2.GetString(6))
                    column_8 = CStr(myreader2.GetString(7))
                    column_9 = CStr(myreader2.GetString(8))
                    column_10 = CStr(myreader2.GetString(9))
                    column_11 = CStr(myreader2.GetString(10))
                    column_12 = CStr(myreader2.GetString(11))
                    column_13 = CStr(myreader2.GetString(12))
                    column_14 = CStr(myreader2.GetString(13))
                    column_15 = CStr(myreader2.GetString(14))
                    column_16 = CStr(myreader2.GetString(15))
                    column_17 = CStr(myreader2.GetString(16))
                    column_18 = CStr(myreader2.GetString(17))
                    column_19 = CStr(myreader2.GetString(18))
                    column_20 = CStr(myreader2.GetString(19))
                    column_21 = CStr(myreader2.GetString(20))
                    column_22 = CStr(myreader2.GetString(21))
                    column_23 = CStr(myreader2.GetString(22))
                    oWrite2.WriteLine("{0}" & t & "{1}" & t & "{2}" & t & "{3}" & t & "{4}" & t & "{5}" & t & "{6}" & t & "{7}" & t & "{8}" & t & "{9}" & t & "{10}" & t & "{11}" & t & "{12}" & t & "{13}" & t & "{14}" & t & "{15}" & t & "{16}" & t & "{17}" & t & "{18}" & t & "{19}" & t & "{20}" & t & "{21}" & t & "{22}", column_1, column_2, column_3, column_4, column_5, column_6, column_7, column_8, column_9, column_10, column_11, column_12, column_13, column_14, column_15, column_16, column_17, column_18, column_19, column_20, column_21, column_22, column_23)
                End While
                oWrite2.Close()
                myreader2.Close()

            End If
        Next
        cn.Close()

        MsgBox("All selected batches have been moved to production.")
        frmPauseMsg.Hide()

        Me.Close()
    End Sub

End Class
0
Fernando SotoRetiredCommented:
Hi T;

Add the Application.DoEvents() call as shown below. That should do the trick
For Each itemChecked In clbBatches.CheckedItems
    '' Go handle any waiting UI events then continue
    Application.DoEvents()
    strBatch = itemChecked.item(0).ToString

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
T HoecherlDeveloperAuthor Commented:
Thanks Fernando.  That did the trick.
0
Fernando SotoRetiredCommented:
Not a problem T; glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.