-
Feb 2nd, 2018, 08:49 AM
#1
Thread Starter
Addicted Member
Design ideas for "wait for" something
I have an industrial control application that uses an environmental test chamber (oven). I need to set the temperature and wait for it to get to setpoint before continuing. Once setpoint is reached, there will be a "soak" time of about 30 minutes before the process continues. I'd like to display the current temperature, sampling the oven every second or two, and then a "countdown" once the soak is started.
My thinking is to just have a do-while loop, with a Threading.Sleep for the one second delay, and do that again for the 30 minute countdown. Is this a viable design idea, or can it be better implemented with a timer event or some other method?
Just kinda thinking out loud here, and asking if you guys have done this before.
-
Feb 2nd, 2018, 08:51 AM
#2
Re: Design ideas for "wait for" something
I maybe to a point in my life where I understand code more than english
-
Feb 2nd, 2018, 09:12 AM
#3
Re: Design ideas for "wait for" something
I would either use a timer or an internal web service that would run independently and get the values for a call.
This way you do not have to be afraid that you continues loops may bloat something.
Or create something with threading but I'n not an expert at that.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 2nd, 2018, 09:45 AM
#4
Re: Design ideas for "wait for" something
How are you communicating with this oven from VB?
-
Feb 2nd, 2018, 10:27 AM
#5
Re: Design ideas for "wait for" something
Your thinking is intuitive, but will only work in a console application. Loops with Thread.Sleep tend to be very primitive attempts at these kinds of algorithms.
What I would do in my current code is I'd write a type that can represent "read the oven's temperature" in a nice way. Then I'd use the Reactive Extensions to make a type that reads periodically and publishes a result. Then my program could subscribe to that observable and manipulate the readings.
What most people are more used to would be:
I could write a type that represents "read the oven's temperature" in a nice way. Then I'd use a Timer to read that temperature periodically and do whatever with the readings.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 2nd, 2018, 12:20 PM
#6
Re: Design ideas for "wait for" something
I'd certainly go with the timer. A one or two second delay is loads of time for a timer. However, if reading the temperature itself is slow, then I might put that and the timer on a different thread. It's probably not all that slow, though.
A timer for the countdown would work, but the imprecision of the timer would come into play over the length of that cool down. I wouldn't count on the timer to count out the 30 minutes. You'd likely end up with a few extra minutes. What you could do is not the beginning time of the soak, then have the timer tick every 200 ms, or so, and update the display with the system time whenever the timer ticked. This would let the system clock do the actual timing of the 30 minutes, in which case you'd be off by, at most, the interval of the timer, so your 30 minute interval would be extended by no more than 200 ms.
My usual boring signature: Nothing
-
Feb 2nd, 2018, 02:50 PM
#7
Re: Design ideas for "wait for" something
You could get away with using just two controls and two components. The two controls would just be visual controls to show the user the progress of the warm up/cool down where as the two components would waiting/count down.
Take a look at this example:
Code:
Public Class Form1
'Controls
Private picture_loading As PictureBox
Private progress_loading As ProgressBar
'Components
Private background_warmup As ComponentModel.BackgroundWorker
Private timer_cooldown As Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Create new instances of the controls
picture_loading = New PictureBox
progress_loading = New ProgressBar
'Set the properties of the controls
With picture_loading
.Dock = DockStyle.Fill
.ImageLocation = "https://upload.wikimedia.org/wikipedia/commons/b/b1/Loading_icon.gif"
.Name = "picture_loading"
.SizeMode = PictureBoxSizeMode.StretchImage
End With
With progress_loading
.ForeColor = Color.Red 'Enable XP Styles = False
.Dock = DockStyle.Bottom
.Minimum = 0 'This should actually be the result of the first time you read the temperature
.Maximum = 100 'This should be the designated temperature
.Name = "progress_loading"
.Value = .Minimum
End With
With Me
.Controls.AddRange({picture_loading, progress_loading})
.Text = ""
End With
'Create new instances of the compontents
background_warmup = New ComponentModel.BackgroundWorker
timer_cooldown = New Timer
'Set the properties of the compontents
background_warmup.WorkerReportsProgress = True
timer_cooldown.Interval = 1000
'Generate the events for the compontents
AddHandler background_warmup.DoWork, Sub(worker As Object, e_args As ComponentModel.DoWorkEventArgs)
Dim temperature As Double = progress_loading.Value
Do Until temperature = progress_loading.Maximum
'TODO: uncomment next line and change function name to your actual read function name
'temperature = ReadTemperature()
temperature += 1 'TODO: remove from production
background_warmup.ReportProgress(Convert.ToInt32(temperature))
Threading.Thread.Sleep(100) 'TODO: consider removing this from production unless you want a delay
Loop
End Sub
AddHandler background_warmup.ProgressChanged, Sub(worker As Object, e_args As ComponentModel.ProgressChangedEventArgs)
progress_loading.Value = e_args.ProgressPercentage
End Sub
AddHandler background_warmup.RunWorkerCompleted, Sub()
With progress_loading
.ForeColor = Color.Aqua
.Value = 30
.Maximum = .Value
End With
timer_cooldown.Start()
End Sub
Dim countdown As Integer = 30
AddHandler timer_cooldown.Tick, Sub()
countdown -= 1
progress_loading.Value -= 1
timer_cooldown.Enabled = countdown > 0
End Sub
'Start the process
background_warmup.RunWorkerAsync()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|