X毫秒后超时的线程 [英] Timing out a thread after X miliseconds

查看:70
本文介绍了X毫秒后超时的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果进程超过了设置的超时时间,我试图使线程超时.我研究了Thread.Join()方法,该方法在超时时间段或线程完成后(以先到者为准)退出线程.
在调试过程中,它命中了thread.Abort()语句,但该线程直到完成处理后才会中止.

例如:线程需要在5秒内超时,但是如果该过程需要20秒,则该线程仍将运行20秒以处理信息.


我改进了问题和示例
[/EDIT]

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.


I have improved the question and examples
[/EDIT]

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





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

推荐答案

我在StackOverflow上发现了以下讨论:
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


这篇关于X毫秒后超时的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆