Results 1 to 3 of 3

Thread: [RESOLVED] 2nd thread does not end when flagged to end.

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2022
    Posts
    5

    Resolved [RESOLVED] 2nd thread does not end when flagged to end.

    Good day,
    When the main thread is busy, I want to display a progress bar and then close it when the main thread is finished work. The display of Form2 and it's progress bar work fine. But when the stopFlag is set, the 2nd thread does not see it and does not execute the Form2.Close() code. It only executes it after I manually close Form2. The below code is a simple app that demonstrates the problem. I am running Win 10 and VS 2019. Any ideas would be appreciated. Thanks
    Code:
    Imports System.Threading
    Public Class Form1
        Dim stopFlag As Boolean = False
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            stopFlag = False
            Dim t = New Thread(AddressOf ShowProgress)
            t.Start()
            Dim y = 0
            For x = 1 To 10000
                While y < 200000
                    y = y + 1
                End While
                y = 0
            Next
            stopFlag = True
            MessageBox.Show("Done")
        End Sub
        Private Sub ShowProgress()
            Form2.ShowDialog()
            Form2.BringToFront()
            While stopFlag = False
            End While
            Form2.Close()
        End Sub
    End Class
    Last edited by Shaggy Hiker; May 27th, 2022 at 09:43 PM. Reason: Added CODE tags.

Tags for this Thread

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