在vb.net中设置超时 [英] Setting Timeout in vb.net

查看:362
本文介绍了在vb.net中设置超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正处于中间状态,我需要设置一些超时以在超时中止一个线程。我需要的是一种方法,我可以设置一个超时的回调函数或子超时指定的超时时间将被调用。



这是一个示例使事情更清晰的代码



Hi, i am in the middle of something and i need to set some timeout to abort a thread in the timeout. What i need is a way i can set a timeout with a callback function or sub that will be called when the specified time of the timeout elapse.

Here is a sample code to make things more clearer

'here, i need to set a callback function that will be called when the time elapse.
Private Sub callbk()
    'code here will be executed when the time elapse
    Threading.Thread.Abort()
End Sub

 
Private Sub btn_start(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_start.Click
'setup a timeout here to call the sub callbk when the time elapse.
'for example, something like this
    myTimeout(5000, AddressOf callbk)
'the first argument is the time interval in milliseconds while the second argument is the function to callback after the period of 5 seconds
    While True
        'do some coding here that can never stop
        'so when the timeout elapse, the thread is aborted
        'by the callback function
    End While
End Sub





所以,我需要帮助!



so, i need help!

推荐答案

经过一系列的研究工作,我终于得到了msdn的解决方案。以下是可以帮助其他人的解决方案:



I finally got the solution from msdn after series of research work. Here is the solution in case it might help other people:

'here, i need to set a callback function that will be called when the time elapse.
Private Sub callbk()
    'code here will be executed when the time elapse
    Threading.Thread.Abort()
End Sub
 
 
Private Sub btn_start(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_start.Click
'setup a timeout here to call the sub callbk when the time elapse.
'for example, something like this
    Dim tcb As TimerCallback = AddressOf callbk
    Dim t As Timer = New Timer(tcb, Nothing, 5000, -1)
'the first argument is the time interval in milliseconds while the second argument is the function to callback after the period of 5 seconds
    While True
        'do some coding here that can never stop
        'so when the timeout elapse, the thread is aborted
        'by the callback function
    End While
End Sub



谢谢!


thanks!


这篇关于在vb.net中设置超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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