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:
Dim StartTime = Datetime.Now
Dim Interval as Double= (input)
While [Condotion]
‘Do task one
If (task1outcome =False)
Exit While
End if
If StartTime.Subtract(DateTime.Now) > Interval Then
‘Do task two
StartTime = Datetime.now
End if
Thread.Sleep(500)’ms
End While
‘—————— Option 2 ————
While [Condition]
‘Do task 2
Thread.Sleep(Interval)
End While
‘The above code would be Run in a task
While [Condition]
‘Do task 1
If (task1outcome = False) Then
Stop Task
Exit While
End If
Thread.Sleep (500) ‘ms
End While
Sorry for the bad Pseudo code, but this was semi bothering me and I’m on my phone.
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".
Re: Separate Task or Time Calculation - Code Suggestion
Quote:
Originally Posted by
jmcilhinney
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:
'Set up timer for task one and timer for task 2
Resetevent.WaitOne()
'Stop Timers and clean up
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?
Re: Separate Task or Time Calculation - Code Suggestion
Quote:
Originally Posted by
jmcilhinney
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:
'Setup
'Start Timer for Task 2 at user defined interval. Usually not a heavy process and usually large interval
While [Condition] ' Monitors status
Thread.Sleep(500) 'ms
End While
'Reaches this point when the condition returns false.
'Stop Timer
'Clean up
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.