Private WithEvents tim As New System.Timers.Timer
    Public Delegate Sub doSub()
    Private thingToDo As doSub

    Public Sub TimeOut(ByVal d As doSub, ByVal milliseconds As Integer)
        thingToDo = d
        tim.AutoReset = False
        tim.Interval = milliseconds
        tim.Start()
    End Sub

    Private Sub tim_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles tim.Elapsed
        Invoke(thingToDo)
    End Sub
    Private Sub WBTimeOut()



        Me.Finalize()

        Me.Dispose()





    End Sub

Here is an example. It is mostly working fine, but after it's dispose often I will get invoke error (not sure why) :/ (not sure why, sometimes it will work 100 times and sometimes it will fail on first disposale...

Is there any other better method to implement timeout ?

Hmm I've tried background worker and timeout.

After a few repetitions for some reason it fails :/

Timeout starts with opening of new form, after 15 seconds if not completed it closes it and starts again. Unfortunately after few times it will show that it run out of time in 1-2 seconds after form is created and timer started.

I'm not sure if backgroundworker isn't disposing along with form or something.

What you want is for a form to open, wait 15 seconds, and if not submitted, close and open the same form again?

Why not just

Public Class Form1

    Private CountDown As Integer

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        CountDown = 15
        Timer1.Interval = 1000
        Timer1.Start()

    End Sub

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        CountDown -= 1

        If CountDown > 0 Then
            Me.Text = CountDown
        Else
            Me.Text = "timer expired"
            'add call to custom sub here
            Timer1.Stop()
        End If

    End Sub

    Private Sub btnReset_Click(sender As System.Object, e As System.EventArgs) Handles btnReset.Click
        Me.Text = CountDown
    End Sub

End Class

It needs to be done from inside of opened form, in addition there is more timeouts in form which are started by code inside of form. Timeout won't always end up with closing form.

There is:

if XXX 
    Timeout1
end if

......

if XXY 
    Timeout2
end if

....

in Timeout1 
    If _shouldclose = true than
    me.close
    end if

etc.

I've tried threads but it keeps refusing to reopen form due to:

An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '----' cannot be instantiated because the current thread is not in a single-threaded apartment.

Form with timeouts is run in loop until job is finished, but form with work has to automatically finish in case of XXX/YYY and timeout has to start after action AAA/BBB.

Any solution I tries is failing, I tried bacrkground worker but for some reason is not working properly after few re-opens/closes of form (timeout sudennly from 90seconds jumps to 2 seconds, I think it has something to do with usage of a delay in a lot of different places and maybe it is just using same class ??)

Ok guys.

I used Thread.Sleep (I had to remove ShowDialog and replace it with Dialog and flag to proceed with code after Dialog is closed, add Invoke for endform sub (I couldn't close it from thread sub).

Seems to be working perfectly fine :)

This question has already been answered. Start a new discussion instead.