Click here to Skip to main content
15,904,494 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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

VB
'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!
Posted
Comments
Sergey Alexandrovich Kryukov 15-Jan-13 11:08am    
Weird, but, in principle, you can do such thing. (Needs a lot of care, can be very dangerous technique...) The problem is how you use the timer. What is "myTimeout"? You don't show its definition. You can use System.Timers.Timer...
—SA
Joeadeoye 15-Jan-13 11:34am    
i just used the myTimeout as an example....

1 solution

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

VB
'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!
 
Share this answer
 

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