|
-
Jan 13th, 2010, 12:45 AM
#8
Thread Starter
Fanatic Member
Re: Threading problem
 Originally Posted by jmcilhinney
The problem is that you're NOT executing that code in a separate thread. You start a new thread and call DoProcessing, then the very first thing that DoProcessing does is invoke a call back to the UI thread and ALL the work is done there.
You are supposed to do all your work on the worker thread and then invoke a call back to the UI thread ONLY to access the UI. Get rid of any that If block and any interaction with the UI from that method. Then that whole method will be executed on the worker thread. You then write a new method ONLY for interaction with the UI, e.g.
vb.net Code:
Private Sub UpdateLabel(ByVal text As String)
If Me.lblstatus.InvokeRequired Then
Me.lblstatus.Invoke(New MethodInvoker(AddressOf DoProcessing), text)
Else
Me.lblstatus.Text = text
End If
End Sub
You can then call that method from DoProcessing to update lblstatus.
Thanks buddy for your help. I removed the invoke call to UI thread statements. The DoProcessing sub now does not directly access the UI.
But still the application behaves like before that is it locks up till the processing isn't over. I am unable to understand why?. 
Code:
Private Sub DoProcessing()
Try
' this function will check for data.
' CHECKS/Validation
If ProgramSettings.CheckForMissedData = False Then Exit Sub
If Now.DayOfWeek = DayOfWeek.Saturday Then Exit Sub
Dim FPath As String = DataPath & "\Abc.txt"
If File.Exists(FPath) = False Then Exit Sub
Dim CTime As DateTime = Now, GoAhead As Boolean = False, Cnt As Integer = 0
If Format$(CTime, "HHmm").ToString.Substring(3, 1) = "5" Or Format$(CTime, "HHmm").ToString.Substring(3, 1) = "0" Then
GoAhead = True ' means we have 5 min in hand and we can go ahead and check
Else
Do
Cnt = Cnt + 1
CTime = CTime.AddMinutes(1)
If Format$(CTime, "HHmm").ToString.Substring(3, 1) = "5" Or Format$(CTime, "HHmm").ToString.Substring(3, 1) = "0" Then Exit Do
If Cnt > 5 Then Exit Do ' EMERGENCY EXIT
Loop
If Cnt >= 3 Then GoAhead = True Else GoAhead = False
End If
If GoAhead = False Then Exit Sub
' Checks Done
Dim FileData() As String = File.ReadAllLines(FPath), FTemp() As String
Dim FileTime As DateTime, FileDate As Date, Ticks() As String, EndTime As DateTime
Dim RecCount As Long = 0, ReadCounter As Long = 0
ReDim Ticks(0)
If UBound(FileData) < 3 Then
Exit Sub ' get out
End If
Call LabelReset("Status: Checking for missed data")
For i As Long = UBound(FileData) To LBound(FileData) Step -1
If FileData(i).Trim = "" Then Continue For
ReadCounter += 1
FileDate = New Date(FileData(i).ToString.Substring(6, 4), FileData(i).ToString.Substring(3, 2), FileData(i).ToString.Substring(0, 2))
RecCount = i
If ReadCounter = 1 Then ' Get End Time
FTemp = FileData(i).Split(",")
EndTime = New DateTime(Now.Date.Year, Now.Date.Month, Now.Date.Day, FTemp(1).Substring(0, 2), FTemp(1).Substring(2, 2), 0)
End If
If Format$(FileDate, "yyyyMMdd") <> Format$(Now.Date, "yyyyMMdd") Then
Exit For
End If
Next
If RecCount = UBound(FileData) Then
Exit Sub
End If
ReadCounter = 0 : RecCount += 1
FileTime = New DateTime(Now.Date.Year, Now.Date.Month, Now.Date.Day, "09", "05", "0")
Do
ReadCounter = ReadCounter + 1
If RecExistsInArray(FileData, RecCount, Format$(FileTime, "HHmm")) = True Then
FileTime = FileTime.AddMinutes(5)
Else
ReDim Preserve Ticks(UBound(Ticks) + 1)
Ticks(UBound(Ticks)) = Format$(FileTime, "HHmm")
FileTime = FileTime.AddMinutes(5)
End If
If FileTime = EndTime Then Exit Do
If ReadCounter > 70 Then Exit Do ' Emergency Exit
Loop
' Done now check if there were missed ticks or not
If UBound(Ticks) = 0 Then
' All ticks present so far
Call LabelReset("Status: No ticks missed so far")
Exit Sub
End If
' NOW DOWNLOAD THOSE TICKS
Dim DownloadedTicks() As String : ReDim DownloadedTicks(0)
For i As Integer = 1 To UBound(Ticks)
Try
If File.Exists(Application.StartupPath & "\" & Ticks(i) & ".dat") = True Then File.Delete(Application.StartupPath & "\" & Ticks(i) & ".dat")
My.Computer.Network.DownloadFile("http://www.mysite.com/" & Format(DateTimePicker1.Value.Date, "ddMMyy") & "/" & Ticks(i) & ".dat", Application.StartupPath & "\" & Ticks(i) & ".dat")
ReDim Preserve DownloadedTicks(UBound(DownloadedTicks) + 1) ' tick downloaded
DownloadedTicks(UBound(DownloadedTicks)) = Ticks(i)
Catch ex As Exception
' means couldnt download or find file
End Try
Next
If UBound(DownloadedTicks) = 0 Then
' didnt find file so quit/exit
' lblstatus.Text = "Status: Missed data check completed" : lblstatus.Refresh()
' commented above
Exit Sub
End If
' Found some/all ticks
' Read data files in array
' Code for Processing File Not Pasted
Call LabelReset("Status: DONE")
Catch ex As Exception
End Try
End Sub
Private Sub LabelReset(ByVal text As String)
If Me.InvokeRequired = True Then
Me.lblstatus.Invoke(New MethodInvoker(AddressOf DoProcessing), text)
Else
lblstatus.Text = text : lblstatus.Refresh()
End If
End Sub
' Timer code
Private Sub TimerMain_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerMain.Tick
If LastCheckOK() = True Then
lblstatus.Text = "Status: Waiting for next data" : lblstatus.Refresh()
TimerMain.Interval = GetTimerInterval()
' calling thread
MissedTickThread = New Thread(AddressOf DoProcessing)
MissedTickThread.Start()
Exit Sub
End If
End Sub
Cheers.
Last edited by greatchap; Jan 13th, 2010 at 07:12 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|