I am trying to get a thread to time out if the process takes over a set Timeout period. I have looked into the Thread.Join() method which exits the thread after the Timeout period or the thread is completed which ever comes first.
In debugging it hits the thread.Abort() statement, but the thread does not abort until its finished processing.
Example: the thread needs to timeout within 5 seconds, but if the process takes say 20 seconds the thread still runs for the 20 seconds processing the information.
[EDIT]
I have improved the question and examples
[/EDIT]
public sub GetDATA()
Dim thread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf LoadSQLData))
thread.Start(New Object() {0, 0, 0, 0})
If Not thread.Join(1000) Then
thread.Abort()
thread = Nothing
MessageBox.Show("Time out!!")
End If
End Sub
Private Sub LoadSQLData(ByVal filterObjects As Object)
If gvResults.InvokeRequired Then
gvResults.Invoke(New Action(Of Object)(AddressOf LoadSQLData), filterObjects)
Return
End If
Dim repItem As Integer = filterObjects(0)
Dim streamItem As Integer = filterObjects(0)
Dim periodItem As Integer = filterObjects(0)
Dim forecastItem As Integer = filterObjects(0)
Try
Dim command = CurrentSession.SalesConnection.GetNewCommand("SP_LoadMainData")
command.Parameters.AddWithValue("@rep", repItem)
command.Parameters.AddWithValue("@stream", streamItem)
command.Parameters.AddWithValue("@period", periodItem)
Dim accounts = CurrentSession.SalesConnection.ExecuteQuery(Of System.Data.DataTable)(command)
gvResults.DataSource = accounts
Catch ex As Exception
Throw ex
Finally
RaiseEvent Load_Complete(gvResults, loadingPic)
End Try
End Sub