Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to run a specific function when the timer reach 50 a message box will show and then the specific functions run, and resets it back to 0, could you help review and add codes that may help me with it?

Note: label1.text is where the timer shows

Appreciate your help.

Thank you in advance

Here's the code I've been working on:

 If label1.text = "50" Then

            MessageBox.Show("Please wait were updating the data table..")
            Timer1.Stop()


            Dim sqlquery As String = "SELECT PT_N_Std_Tuning, PT_TimeIn, PT_TimeOut, Variant FROM tbl_V9_Datalog WHERE Variant = '5' AND dateadd(hour, -1, dateadd(hour,datediff(hour,0,getdate()),0)) <= [PT_TimeIn]
            and PT_TimeIn < dateadd(hour,datediff(hour,0,getdate()),0) ORDER BY PT_TimeIn ASC"
            

            Dim reader As SqlDataReader
            Dim adapter As New SqlDataAdapter
            Dim command As New SqlCommand
            command.CommandText = sqlquery
            command.Connection = conn1
            adapter.SelectCommand = command
            'command.Parameters.Add("@variantno", SqlDbType.VarChar).Value = cmbox_variant.Text

            reader = command.ExecuteReader


            Dim DataTableName As String = textbox1.text
            Dim dValue As Double
            Dim ID1 As String = ""
            Dim ID2 As String = ""
            Dim ID3 As String = ""
            Dim ID4 As String = ""
            Dim ID5 As String = ""
            Dim ID6 As String = ""
            Dim ID7 As String = ""
            Dim ID8 As String = ""
            Dim ID9 As String = ""
            Dim ID10 As String = ""
            Dim ID11 As String = ""
            Dim ID12 As String = ""
            Dim Note1 As String = ""
            Dim Note2 As String = ""
            Dim sDate As String = ""
            Dim sTime As String = ""
            Dim wd As Int32 = S2K_SetDataTableVariableValue(DataTableName, dValue, ID1, ID2, ID3, ID4, ID5, ID6, ID7, ID8, ID9, ID10, ID11, ID12, Note1, Note2, sDate, sTime)
            Try

                If reader.HasRows Then
                    While reader.Read()
                        dValue = (Val(reader.Item("PT_N_Std_Tuning").ToString))
                        DataTableName = TextBox1.Text
                        ID1 = reader.Item("PT_TimeIn").ToString
                        ID2 = ""
                        ID3 = ""
                        ID4 = ""
                        ID5 = ""
                        ID6 = ""
                        ID7 = ""
                        ID8 = ""
                        ID9 = ""
                        ID10 = ""
                        ID11 = ""
                        ID12 = ""
                        Note1 = "" 'reader.Item("AttemptNo").ToString
                        Note2 = ""
                        Dim sDate1 = reader.GetOrdinal("PT_TimeOut")
                        sDate = reader.GetDateTime(sDate1).ToShortDateString
                        Dim sTime1 = reader.GetOrdinal("PT_TimeOut")
                        sTime = reader.GetDateTime(sTime1).ToLongTimeString
                        wd = S2K_SetDataTableVariableValue(DataTableName, dValue, ID1, ID2, ID3, ID4, ID5, ID6, ID7, ID8, ID9, ID10, ID11, ID12, Note1, Note2, sDate, sTime)
                    End While


                    MessageBox.Show("Data has been updated")
                End If 
End If


What I have tried:

Please refer on the codes, I have used the label1.text but it does not run the function after reaching the 50.
Posted
Updated 15-Jul-19 22:00pm

1 solution

Instead of using labels, set up a class level DateTime, and set it to the current time + 50 seconds - DateTime.AddSeconds will do that (assuming that's 50 seconds you are trying to time for, other "AddXXX" methods are available)
Then, in your Timer.Tick event, check the current time against the expiry time, and if it's greater of equal, stop the timer, and display your message.
Or better, don't stop the timer, just set the Expiry time to DateTime.MaxValue which will have the same effect, but means the Timer can be reused later if necessary (Timers are a scarce resource, so you don't want to generate more than you need).
If necessary, you can set the label text to the difference between DateTime.Now and the expiry time instead of trying to increase a string based value.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900