Click here to Skip to main content
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 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
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