G
Guest
I seem to run into a Catch 22 problem updating forms from a second thread. I have only been able to get the Invoke() function to work when the form was displayed using Application.Run(TheForm). When using ShowDialog() to display the form the Invoke() function does not return until the form closes. However, The application loses focus if I use Application.Run(TheNextForm) after the first one is closed.
The following code does not have the synchorization elements necessary to be rigorously correct but it demonstrates the problems I'm seeing even with proper synchronization. BTW, the forms are default created forms with MinimizeBox = False so I'm using the 'X' to close the form. I've tried lots of combinations of ShowDialog(frm) and Application.Run(frm) with one or the other problem occuring. Am I missing something?
Thanks in advance
Imports System.Threading
Public Class FocusClass
Private Shared f As Form
Public Shared Sub Main()
Dim f1 As Focus1
Dim f2 As Focus2
f1 = New Focus1
Application.Run(f1)
Dim th As New Thread(AddressOf UpdateLoop)
th.Start()
Do
f2 = New Focus2
f = f2
f2.ShowDialog() ' I've also tried Applicaiton.Run(f2)
f1 = New Focus1
f = f1
f1.ShowDialog() ' I've also tried Applicaiton.Run(f1)
Loop
End Sub
Private Shared Sub UpdateLoop()
Do
Thread.Sleep(500)
On Error Resume Next
f.Invoke(New EventHandler(AddressOf
UpdateItem))
On Error GoTo 0
Loop
End Sub
Private Shared Sub UpdateItem(ByVal sender As Object,
ByVal e As EventArgs)
f.Text = Format(Now, "hh:mm:ss")
End Sub
End Class
The following code does not have the synchorization elements necessary to be rigorously correct but it demonstrates the problems I'm seeing even with proper synchronization. BTW, the forms are default created forms with MinimizeBox = False so I'm using the 'X' to close the form. I've tried lots of combinations of ShowDialog(frm) and Application.Run(frm) with one or the other problem occuring. Am I missing something?
Thanks in advance
Imports System.Threading
Public Class FocusClass
Private Shared f As Form
Public Shared Sub Main()
Dim f1 As Focus1
Dim f2 As Focus2
f1 = New Focus1
Application.Run(f1)
Dim th As New Thread(AddressOf UpdateLoop)
th.Start()
Do
f2 = New Focus2
f = f2
f2.ShowDialog() ' I've also tried Applicaiton.Run(f2)
f1 = New Focus1
f = f1
f1.ShowDialog() ' I've also tried Applicaiton.Run(f1)
Loop
End Sub
Private Shared Sub UpdateLoop()
Do
Thread.Sleep(500)
On Error Resume Next
f.Invoke(New EventHandler(AddressOf
UpdateItem))
On Error GoTo 0
Loop
End Sub
Private Shared Sub UpdateItem(ByVal sender As Object,
ByVal e As EventArgs)
f.Text = Format(Now, "hh:mm:ss")
End Sub
End Class