Timer for Test Timeout + General Code Structure
-
Hi 1) Can anyone help me with how to structure some code that if something has not happened in a certain time, the function will return 'False'. The example 'Test0' below must be bad because I am constantly sitting in a loop checking the Port Pin. 2) Is there a better way to use a Timer in 'Test1'? 3) Which routines should be at the Top of the code. Since the routine 'DoTests' is the main loop, I have put it at the top - then as the tests are called, so you find the routines further down in the code. It obviously works anyway around, but I have often noticed in other code examples that the main routine is at the bottom of all the code. 4) After the tests are done (Pass or fail, the routine 'DisplayError is called from the 'DoTests' routine. 'Display Error' in turn calls 'DoTests'. In other words they are constantly calling each other. Is this bad? Any comments on how I can make this code better will be appreciated.. Example: Private Sub DoTests() Test0() If Not Test1() Then DisplayError(1) End If If Not Test2() Then DisplayError(2) End If DisplayError(10) 'All Tests Passed End Sub 'Loop until Jig Closes (D0 = 0) to start tests Private Function Test0() As Boolean Do While LPTPort.GetState(D0) = 1 Loop Return True End Function Private Function Test1() As Boolean Dim tmrTestTimeout As Windows.Forms.Timer tmrTestTimeout.Interval = 3000 tmrTestTimeout.Enabled = True 'Wait for LPTPort pin D1 to go low OR timeout Do While tmrTestTimeout.Enabled If LPTPort.GetState(D1) = 0 Then tmrTestTimeout.Enabled = False Return True End If Loop 'Timed out - so Return False tmrTestTimeout.Enabled = False Return False End Function Private Sub DisplayError(ByVal intErrorCode As Integer) Select Case (intErrorCode) Case 1 Me.lblTestStatus.Text = "Test 1 Failed" Case 2 Me.lblTestStatus.Text = "Test 2 Failed" Case 10 Me.lblTestStatus.Text = "All Tests Passed" Case Else Me.lblTestStatus.Text = "Unknown Error - Test Aborted" End Select 'Wait for Jig to open (D0 = 0) so last unit tested is removed Do While LPTPort.GetState(D0) = 1 Loop DoTests() End Sub
-
Hi 1) Can anyone help me with how to structure some code that if something has not happened in a certain time, the function will return 'False'. The example 'Test0' below must be bad because I am constantly sitting in a loop checking the Port Pin. 2) Is there a better way to use a Timer in 'Test1'? 3) Which routines should be at the Top of the code. Since the routine 'DoTests' is the main loop, I have put it at the top - then as the tests are called, so you find the routines further down in the code. It obviously works anyway around, but I have often noticed in other code examples that the main routine is at the bottom of all the code. 4) After the tests are done (Pass or fail, the routine 'DisplayError is called from the 'DoTests' routine. 'Display Error' in turn calls 'DoTests'. In other words they are constantly calling each other. Is this bad? Any comments on how I can make this code better will be appreciated.. Example: Private Sub DoTests() Test0() If Not Test1() Then DisplayError(1) End If If Not Test2() Then DisplayError(2) End If DisplayError(10) 'All Tests Passed End Sub 'Loop until Jig Closes (D0 = 0) to start tests Private Function Test0() As Boolean Do While LPTPort.GetState(D0) = 1 Loop Return True End Function Private Function Test1() As Boolean Dim tmrTestTimeout As Windows.Forms.Timer tmrTestTimeout.Interval = 3000 tmrTestTimeout.Enabled = True 'Wait for LPTPort pin D1 to go low OR timeout Do While tmrTestTimeout.Enabled If LPTPort.GetState(D1) = 0 Then tmrTestTimeout.Enabled = False Return True End If Loop 'Timed out - so Return False tmrTestTimeout.Enabled = False Return False End Function Private Sub DisplayError(ByVal intErrorCode As Integer) Select Case (intErrorCode) Case 1 Me.lblTestStatus.Text = "Test 1 Failed" Case 2 Me.lblTestStatus.Text = "Test 2 Failed" Case 10 Me.lblTestStatus.Text = "All Tests Passed" Case Else Me.lblTestStatus.Text = "Unknown Error - Test Aborted" End Select 'Wait for Jig to open (D0 = 0) so last unit tested is removed Do While LPTPort.GetState(D0) = 1 Loop DoTests() End Sub
I think it is a much better idea to have an actual counter in the loop and only wait a max amount of time. It also is a good idea to Application.DoEvents while you are in a loop or your form won't refresh. dim cnt as Integer = 0 Do While (Your normal check here) AndAlso cnt < 100000 'do your other code here System.Threading.Thread.Sleep(500) Application.DoEvents cnt = cnt + 1 Loop If cnt >= 100000 Then 'didn't finish the test... End If I would suggest that type of code over a timer. That way you know for sure that you will exit the loop and with the application.DoEvents it won't look like your application has died and isn't coming back. Hope that helps. Ben