L
Lucvdv
Can anyone explain why this happens with the code at the bottom?
It looked like a thread safety issue, but changing the declaration of
Label1 to Shared doesn't help.
Standard windows form; one label, two buttons.
- if Label1.Visible is set to True in the form designer, everything
works as expected. Button1 toggles the label visibility, and Button2
flashes it on for 250 msec.
- if Label1.Visible is set to False in the form designer and the
Button2_Click line in Form1_Load is commented out so the new thread
isn't started automatically at form load, everything still works the
same.
- if Label1.Visible is set to False in the form designer and the
Button2_Click line in Form1_Load is left enabled, the label always
remains hidden, regardless of the state of its Visible property.
The debug output shows that the property value flips between false and
true when Button1 is clicked, but the label remains invisible.
This code is simplified to the bare bones needed to reproduce the
problem:
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
[+] [ Windows Form Designer generated code ]
Private Sub ThreadProc()
Label1.Visible = True
Thread.Sleep(250)
Label1.Visible = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button2_Click(Nothing, Nothing)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Visible = Not Label1.Visible
Debug.WriteLine(Label1.Visible)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim thr As New Thread(AddressOf ThreadProc)
thr.Start()
End Sub
End Class
In the real application it's a servername validity test for an SQL
connection, it checks if the server exists and if the database exists
on the server. It's implemented as a separate thread to prevent
network problems from freezing the UI for the time-out period. The
label is used to indicate that the test is still in progress.
A mutex synchronizes everything that's accessed by more than one
thread there (i.e. the label and a few variables), it was left out
here because having it or not doesn't change anything to the problem.
It looked like a thread safety issue, but changing the declaration of
Label1 to Shared doesn't help.
Standard windows form; one label, two buttons.
- if Label1.Visible is set to True in the form designer, everything
works as expected. Button1 toggles the label visibility, and Button2
flashes it on for 250 msec.
- if Label1.Visible is set to False in the form designer and the
Button2_Click line in Form1_Load is commented out so the new thread
isn't started automatically at form load, everything still works the
same.
- if Label1.Visible is set to False in the form designer and the
Button2_Click line in Form1_Load is left enabled, the label always
remains hidden, regardless of the state of its Visible property.
The debug output shows that the property value flips between false and
true when Button1 is clicked, but the label remains invisible.
This code is simplified to the bare bones needed to reproduce the
problem:
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
[+] [ Windows Form Designer generated code ]
Private Sub ThreadProc()
Label1.Visible = True
Thread.Sleep(250)
Label1.Visible = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Button2_Click(Nothing, Nothing)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Visible = Not Label1.Visible
Debug.WriteLine(Label1.Visible)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim thr As New Thread(AddressOf ThreadProc)
thr.Start()
End Sub
End Class
In the real application it's a servername validity test for an SQL
connection, it checks if the server exists and if the database exists
on the server. It's implemented as a separate thread to prevent
network problems from freezing the UI for the time-out period. The
label is used to indicate that the test is still in progress.
A mutex synchronizes everything that's accessed by more than one
thread there (i.e. the label and a few variables), it was left out
here because having it or not doesn't change anything to the problem.