Click here to Skip to main content
15,922,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]
VB
public sub GetDATA() 
    ' creates the thread which is to process the data
    Dim thread As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf LoadSQLData))
    
    ' starts the thread (with the passed parameters)
    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


VB
Private Sub LoadSQLData(ByVal filterObjects As Object)
       '' invokes the grid control onto the current thread
       If gvResults.InvokeRequired Then
           gvResults.Invoke(New Action(Of Object)(AddressOf LoadSQLData), filterObjects)
           Return
       End If

       '' sets the param values (have placed them as integers for the purpose of the example)
       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
           ' gets the information based on the values passed
           ' CurrentSession.SalesConnection is a custom class which gets the data from the database
           ' Getting the data from the database works fine
           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
Posted
Updated 1-Jun-12 5:43am
v3
Comments
Sergey Alexandrovich Kryukov 1-Jun-12 10:27am    
You should start the question with explaining why. And your solution should be commented, explained and motivated. Please think about making a shorter code sample (yes, for the sole purpose of asking the question and resolving one particular problem, but make this sample complete). If you can do it all, please use "Improve question" above.
--SA
MugiwaraUK 1-Jun-12 11:41am    
Here is the updated code snippet

1 solution

I found this discussion on StackOverflow: Implement C# Generic Timeout[^]. Be sure to read all the solutions that were given and pick the one you're more comfortable with.

Regards,

Manfred
 
Share this answer
 
v2

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