loop while problem RRS feed

  • Question

  • Hello
    I write a code that does not work.
    in fact I want to continually perform a task as long as the form is opened
    I used the while loop

    public f_opened as boolean =true 
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    End Sub     
    Private Sub current_task() 
     While (f_opened = True)
    ' execute task           
     end while
      End Sub

    Monday, May 21, 2018 10:11 AM

All replies

  • I don't see why it does not work, it should be. Probably you cannot intercept it anymore because your computer is doing nothing else than this task every nanosecond. 

    Try a windows forms timer instead. 

    a sample is included in this official page


    Monday, May 21, 2018 10:15 AM
  • the use of the timer does not meet my expectations
    Monday, May 21, 2018 10:32 AM
  • With your code, there is never shown a form, because it is looping and it never stops and therefore the next method is not done. 

    This is so basic about programming. 

    However, crazy that a timer does not meet your expectations, sound like a farmer who had always used a horse before his cart and therefore an engine in his new Ferrari did not meet his expectations.  


    Monday, May 21, 2018 10:53 AM
  • i see
    Monday, May 21, 2018 11:04 AM
  • Jacob,

    You still have an open thread here.  This seems to be a continuation of that original issue.  You should close an existing thread before starting a new one.  You close a thread by clicking the Mark as answer link at the bottom of one or more appropriate posts.

    Note that your code above does not use the Async keyword or appear to contain any async code.  You can't start a long running process on the main thread from Form.Load without using asynchronous code.

    Did you review the article that I pointed you to in your other thread?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, May 21, 2018 11:39 AM
  • A slightly different approach

        Public f_closed As New Threading.ManualResetEvent(False) 'replaces f_opened
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            f_closed.Set() 'indicate closing
            _curTask.Wait() 'wait for back ground task to end
        End Sub
        Private _curTask As Task
        Private Sub current_task()
            _curTask = Task.Run(Sub()
                                    Const loopDlyMS As Integer = 10 'delay between loop executions
                                        'loop code here
                                    Loop While Not f_closed.WaitOne(loopDlyMS)
                                End Sub)
        End Sub

    This will run the code in a background thread.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Monday, May 21, 2018 2:39 PM
  • the use of the timer does not meet my expectations

    Well you would need to execute a loop that seems almost infinite on a separate thread to keep the UI from freezing.

    Regardless you should really have a delay in the loop. What would the loop be used for?

    Perhaps the best thing would you provide what your app is to do so others could assist you with a more complete solution.

    La vida loca

    Monday, May 21, 2018 3:25 PM