pause application

leontager

Well-known member
Joined
Jun 17, 2003
Messages
89
hi, I have a main loop in my program and it is constantly doing something. I would like to add a button on my interface which will pause the loop on click and resume from the same point when i click on it again. how could i do that?
 
You could run the loop in a thread, and suspend execution of the thread and then resume the execution when youre ready again.
 
but if im running a loop how can i make the button still avaialbe. whenever i have the loop nothing is reacting.
 
Look at these three functions. Theres a start and stop button and a loop that increments a counter to a label. Copy the code into a project and youll see how it works.:

Code:
    Initialize the thread, and assign the method to run in the thread
    Private objTheThread1 As System.Threading.Thread

    The start button was pressed
    Private Sub btnStart1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart1.Click
        If Not objTheThread1 Is Nothing Then
            resume the thread if its running
            If objTheThread1.ThreadState = Threading.ThreadState.Suspended Then
                objTheThread1.Resume()
            End If
        Else
            start the thread
            Dim objThread As New System.Threading.Thread(AddressOf RunLoop1)
            objTheThread1 = objThread
            objThread.Start()
        End If
    End Sub

    The pause button was pressed
    Private Sub btnStop1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop1.Click
        If Not objTheThread1 Is Nothing Then
            If objTheThread1.IsAlive Then
                objTheThread1.Suspend()
            End If
        End If
    End Sub


    Private Sub RunLoop1()
        Dim i As Integer

        i = 0
        Do While i >= 0
            i = i + 1
            lblStatus1.Text = i.ToString
            Application.DoEvents()
        Loop
    End Sub
 
Back
Top