Results 1 to 7 of 7

Thread: Design ideas for "wait for" something

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2015
    Location
    Connecticut, USA
    Posts
    147

    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.

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Design ideas for "wait for" something

    I maybe to a point in my life where I understand code more than english

  3. #3
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Design ideas for "wait for" something

    How are you communicating with this oven from VB?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

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

    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

  7. #7
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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