Multiple UI threads (application.Doevents in second thread)

  • Thread starter Thread starter Armin Zingler
  • Start date Start date
A

Armin Zingler

Hello group, (VB 2003, Framework 1.1)

when Application.DoEvents is called in a second UI thread, it is not
possible to close the Form when Application.DoEvents is used. It's simple to
reproduce:

1. Create a new WindowsApplication and add two Buttons to the Form.
2. Add this code to the Form:

Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click

Dim t As New Threading.Thread(AddressOf ThreadStart)
t.Start()
t.Join()
End Sub

Private Sub ThreadStart()
Dim f As New Form1
f.ShowDialog()
End Sub

Private Sub Button2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button2.Click

Do
Application.DoEvents()
Loop While Me.IsHandleCreated
End Sub

3. Start
4. Click Button1 to show the second Form in the second thread.
5. Click Button2 on the second form to start the loop.
6. Try to close the Form using the X in the top-right of the Form

problem #1: The first time trying to click the X behaves as if clicking
button2 again. I consider it to be a bug.

problem #2: The Form can not be closed. My question is: Why? The loop is not
the problem. Restart the application, do *not* show the second for, but only
click button2. Now the form can be closed. Why not if the code is running in
a different thread? Everything else works in the second thread.
 
You need to call DoEvents on the same thread your UI is running on. In fact,
all operations with the UI need to occur on the same thread it was created
on.

It sounds like you have some kind of loop happening in your form and you
want to use a second thread containing a loop with calls to DoEvents so that
your UI remains responsive. For this, you need to move the loop that is
blocking your UI into the second thread and use the Invoke method to
communicate back to the UI.

Jeff
 
Thanks for your reply.
You need to call DoEvents on the same thread your UI is running on.

I do call DoEvents on the same thread.
In fact, all operations with the UI need to occur on the same thread
it was created on.

I know, and that's what I already do
It sounds like you have some kind of loop happening in your form and
you want to use a second thread containing a loop with calls to
DoEvents so that your UI remains responsive. For this, you need to
move the loop that is blocking your UI into the second thread and use
the Invoke method to communicate back to the UI.

I do not need to communicate to the UI using Invoke because the code is
already running in the same thread. There are two instances of the form. One
is running in the main UI thread, the second is running in the second UI
thread.

....

I did some more testing now. It turns out to be not a multi-threading issue.
The difference was that the first Form was shown modeless, the second
modally. I'll open a new thread (in the sense of thread in this news group
*g*) because the subject doesn't fit anymore. Could you please follow me
over there? :-)
 
Back
Top