Click here to Skip to main content
11,478,562 members (68,998 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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!
Posted 15-Jan-13 5:52am
Comments
Sergey Alexandrovich Kryukov at 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 at 15-Jan-13 11:34am
   
i just used the myTimeout as an example....

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 289
1 F-ES Sitecore 170
2 Frankie-C 120
3 OriginalGriff 105
4 DamithSL 95
0 Sergey Alexandrovich Kryukov 7,865
1 OriginalGriff 7,341
2 Sascha Lefèvre 3,059
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100