0

I've got a vb app in .Net 4 that i'm working on. The app fires up dynamic threads to run some jobs. Each thread runs a certain job and within that thread, it performs a infinate loop. It needs to be this way, i can't have the thread and then begin again...

anyway.. i was using threading.thread.sleep in the loop to have the loop pause for a bit before running again. It seems that this is not a good way to go and not recommended. So i thought perhaps i could use a dynamic timer or something else to have the thread wait for say 30 seconds before running again. i was hoping someone could give me some direction and example code.

thanks shannon

~~~~~~~~~~edit hey.. i figured out the problem and no.. it wasn't the sleep in the background thread.... it was a sleep i didn't realize i put in in the UI thread.. I started commented out everything and started adding in 1 by 1 and found it. Sorry PEBKAM error...

Thanks for the input

7
  • Why have you concluded that Thread.Sleep is not a good way of doing it ? as long as the thread isn't the UI thread thread.sleep is just fine :) Commented Feb 29, 2012 at 13:17
  • well, that's what i thought too.. but when i startup my threads, for a few sleep states of the background worker threads.. the UI thread is responsive, but after a few more sleep cycles, the UI starts to get real choppy and then doesn't respond. i had read a couple of posts saying that this was likely the thread.sleep.. that's why i was going down that path Commented Feb 29, 2012 at 13:29
  • @sjums - there is some noise on the groups that suggests that sleep() is an anti-pattern. There is some justification in that sleep() loops are often used as lame form of inter-thread comms, (and also managed threads are aparrently so expensive that sleeping in one is seen in the same light as an idle jetliner - a massive waste of resources. Kinda like a .NET theme, isn't it:). Commented Feb 29, 2012 at 13:32
  • just for grins and went in and commented out the thread.sleep inside the background worker thread and the UI stays responsive... I understand that that wasn't a great test.. but would lead me to believe that the sleep is causing me some problems Commented Feb 29, 2012 at 13:33
  • @jvcoach23 - 'this was likely the thread.sleep' - no, it's not, (not in itself, anyway). Commented Feb 29, 2012 at 13:33

1 Answer 1

1

Use the following class to do the delay, i developed with same concept of javascript:settimeout :

Public Class JSsetTimeout

    Public res As Object = Nothing
    Dim WithEvents tm As Timer = Nothing
    Dim _MethodName As String
    Dim _args() As Object
    Dim _ClassInstacne As Object = Nothing

    Public Shared Sub SetTimeout(ByVal ClassInstacne As Object, ByVal obj As String, ByVal TimeSpan As Integer, ByVal ParamArray args() As Object)
        Dim jssto As New JSsetTimeout(ClassInstacne, obj, TimeSpan, args)
    End Sub

    Public Sub New(ByVal ClassInstacne As Object, ByVal obj As String, ByVal TimeSpan As Integer, ByVal ParamArray args() As Object)
        If obj IsNot Nothing Then
            _MethodName = obj
            _args = args
            _ClassInstacne = ClassInstacne
            tm = New Timer With {.Interval = TimeSpan, .Enabled = False}
            AddHandler tm.Tick, AddressOf tm_Tick
            tm.Start()
        End If
    End Sub

    Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
        tm.Stop()
        RemoveHandler tm.Tick, AddressOf tm_Tick
        If Not String.IsNullOrEmpty(_MethodName) AndAlso _ClassInstacne IsNot Nothing Then
            res = CallByName(_ClassInstacne, _MethodName, CallType.Method, _args)
        Else
            res = Nothing
        End If
    End Sub
End Class

Usage

JSsetTimeout.SetTimeout(ClassContainingMethod, "MethodName", 30000, OptionalParameters)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.