Results 1 to 6 of 6

Thread: Separate Task or Time Calculation - Code Suggestion

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Separate Task or Time Calculation - Code Suggestion

    Hi,

    This might be more personal preference but i’d Like to know peoples ideas on why they’d do this problem one way or the other.

    All of this is happening on a separate background thread, so you don’t have to worry about blocking the UI or anything.

    In this thread I have 2 tasks to do:

    1. has to be done every 0.5Seconds

    2. Can vary between needing to be done once every 1 second or every 30 seconds.

    The two options I see
    VB Code:
    1. Dim StartTime = Datetime.Now
    2. Dim Interval as Double= (input)
    3. While [Condotion]
    4.    ‘Do task one
    5.     If (task1outcome =False)
    6.        Exit While
    7.     End if
    8.    If StartTime.Subtract(DateTime.Now) > Interval Then
    9.      ‘Do task two
    10.       StartTime = Datetime.now
    11.  
    12.    End if
    13.    Thread.Sleep(500)’ms
    14.  
    15. End While
    16.  
    17. ‘—————— Option 2 ————
    18.  
    19. While [Condition]
    20.   ‘Do task 2
    21.   Thread.Sleep(Interval)
    22. End While
    23.  
    24. ‘The above code would be Run in a task
    25.  
    26. While [Condition]
    27.  ‘Do task 1
    28.   If (task1outcome = False) Then
    29.       Stop Task
    30.       Exit While
    31.   End If  
    32.   Thread.Sleep (500) ‘ms
    33. End While

    Sorry for the bad Pseudo code, but this was semi bothering me and I’m on my phone.
    Last edited by Crzyrio; Oct 15th, 2018 at 10:23 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Separate Task or Time Calculation - Code Suggestion

    I wouldn't use a loop and Thread.Sleep at all. I would use a Timers.Timer, which raises its Elapsed event on a secondary thread by default. Given that there are only two tasks and their differing schedule, I would use two Timers. If there was only one schedule or a large number of tasks then I'd probably go for a single Timer and, in the latter case, use some logic to determine which task(s) to perform.

    By the way, it is "pseudo", not "sudo".

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Separate Task or Time Calculation - Code Suggestion

    Quote Originally Posted by jmcilhinney View Post
    I wouldn't use a loop and Thread.Sleep at all. I would use a Timers.Timer, which raises its Elapsed event on a secondary thread by default. Given that there are only two tasks and their differing schedule, I would use two Timers. If there was only one schedule or a large number of tasks then I'd probably go for a single Timer and, in the latter case, use some logic to determine which task(s) to perform.

    By the way, it is "pseudo", not "sudo".
    Thanks! thats interesting, I never really thought of that.

    Would you still choose this route if:

    The initial thread will only exit when one of the task's determines it can at which point the two timers would stop and the task would continue.

    Use a AutoResetEvent that is set by the task?

    VB Code:
    1. 'Set up timer for task one and timer for task 2
    2.  
    3. Resetevent.WaitOne()
    4.  
    5. 'Stop Timers and clean up

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Separate Task or Time Calculation - Code Suggestion

    Perhaps I misunderstood. I was assuming that there was a UI but post #3 seems to suggest that there's not. Can you clarify?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Separate Task or Time Calculation - Code Suggestion

    Quote Originally Posted by jmcilhinney View Post
    Perhaps I misunderstood. I was assuming that there was a UI but post #3 seems to suggest that there's not. Can you clarify?
    Nope, not UI Thread. Sorry should have stated.

    The User Clicks a button on the UI. This button results in the creation of this background thread. The UI refreshs with log details while we process in the bac

    In this background thread:

    • Software does a bit of set up to control external hardware
    • First task with set interval of 0.5s is monitoring status of the system. If parameter is out of line it has to stop the thread
    • Second task interval is defined by the user and does something similar to logging


    Might give this a try. I like the idea of the timer for Task 2

    VB Code:
    1. 'Setup
    2.  
    3. 'Start Timer for Task 2 at user defined interval. Usually not a heavy process and usually large interval
    4.  
    5. While [Condition] ' Monitors status
    6.    Thread.Sleep(500) 'ms
    7. End While
    8.  
    9. 'Reaches this point when the condition returns false.
    10.  
    11. 'Stop Timer
    12.  
    13. 'Clean up

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Separate Task or Time Calculation - Code Suggestion

    I like the idea of a timer for task 2, as well, but I'm fine with the Thread.Sleep for Task 1, since this is a background thread. Sleep is pretty harmless, and that task appears to be running continually at set intervals, so the advantage to keeping it in a single loop seems pretty clear. Task 2 isn't so routine, so a timer makes more sense for that one.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width