Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<hello everyone i have application that calls a function at a specific time.The problem is it repeats the same function and it does not get out of the loop eg if its calls a function at 10:00 am it will keep on calling the same function until time changes to 10:01..


here is my code below




VB
Dim dt As DateTime = DateTime.Now


      Dim strHours As String
      Dim strMinutes As String

      ' Dim inTDay
      strTime = dt.ToString()
      ' inTDay = dt.DayOfWeek
      strHours = dt.Hour.ToString()
      strMinutes = dt.Minute.ToString()
      strTime = strHours & ":" & strMinutes

If dt.DayOfWeek <> DayOfWeek.Saturday Then
          If dt.DayOfWeek <> DayOfWeek.Sunday Then




                  For i As Integer = 0 To ListView1.Items.Count - 1
                  subone = ListView1.Items(i).SubItems(2).Text

                  subone = Microsoft.VisualBasic.Right(subone, 11)
                  subone = Microsoft.VisualBasic.Left(subone, 5)
                  subtwo = ListView1.Items(i).SubItems(3).Text

                  If strTime = subone And subtwo = "Y" Then
                      strFunction = ListView1.Items(i).SubItems(1).Text




                      Call Select_Jobs()

                      If strTime = subone Then
                          Exit For
                      End If
                      'H
                      '
                      '





                  End If



              Next
Posted
Comments
Pheonyx 18-Sep-13 6:02am    
What sort of values are you getting in "subone" ?
What does "Select_Jobs()" do ?
Does the program ever get into the if statement "strTime = subone And subtwo = "Y" "?
Member 10250527 18-Sep-13 6:31am    
The subone has the time and the select_job() selects jobname from the list view then it passes the values to the subroutines...yes it gets into the if statment "strTime = subone And subtwo = "Y".But the problem is when it gets to strTime it will call the same method until time changes.
[no name] 18-Sep-13 6:23am    
Okay.... so what is the question?
Member 10250527 18-Sep-13 6:38am    
I have application that calls specific subroutines at the certain time on the listview...(jobid.jobName.Time.Status)so my loop keeps on repeating same jobname at the same time wen it runs.
[no name] 18-Sep-13 7:33am    
Yes I see that that very odd functionality. So it's doing exactly what you told it to do. Did you have some sort of a question about it?

1 solution

I would store the last time your function was called in a global variable, and check it before attempting to run the code in this function.

For instance, in a module somewhere:

VB
Public lastRun as String


Then your code would look something like:

VB
Dim dt As DateTime = DateTime.Now
 

Dim strHours As String
Dim strMinutes As String
 
' Dim inTDay
strTime = dt.ToString()
' inTDay = dt.DayOfWeek
strHours = dt.Hour.ToString()
strMinutes = dt.Minute.ToString()
strTime = strHours & ":" & strMinutes

If lastRun <> strTime Then
   lastRun = strTime
   ' The rest of your function's code here.
Else
   ' You should do a short Thread.Sleep() here, or if this is on the UI thread an
   ' Application.DoEvents(). If you don't, then while your app is checking to see if it 
   ' should run your function (for the minute), one of your CPUs will be pegged, or
   ' if this is in the UI thread, then the UI will be unresponsive.
End If


This way your code will only run once when the target time is reached.

- Pete
 
Share this answer
 
v6
Comments
Member 10250527 18-Sep-13 9:23am    
Thank you very much it worked,i appreciate the help, i have been struggling with this for a while...
pdoxtader 18-Sep-13 9:27am    
Glad it worked out for you.

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