How to use control 'Invoke()' from second thread - 'Catch 22'

  • Thread starter Thread starter Guest
  • Start date Start date
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 advanc

Imports System.Threadin
Public Class FocusClas
Private Shared f As For
Public Shared Sub Main(
Dim f1 As Focus
Dim f2 As Focus

f1 = New Focus
Application.Run(f1

Dim th As New Thread(AddressOf UpdateLoop
th.Start(
D
f2 = New Focus
f = f
f2.ShowDialog() ' I've also tried Applicaiton.Run(f2

f1 = New Focus
f = f
f1.ShowDialog() ' I've also tried Applicaiton.Run(f1

Loo
End Su

Private Shared Sub UpdateLoop(
D
Thread.Sleep(500

On Error Resume Nex
f.Invoke(New EventHandler(AddressOf
UpdateItem)
On Error GoTo
Loo
End Su

Private Shared Sub UpdateItem(ByVal sender As Object,
ByVal e As EventArgs
f.Text = Format(Now, "hh:mm:ss"
End Su

End Clas
 
Are you using SP2? It was a known bug with a calling Invoke to modal forms
that should've benn fixed in SP2.

--
Alex Yakhnin, NET CF MVP
IntelliProg, Inc.
http://www.intelliprog.com

Kohn said:
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?
 
Hi,

Control.Invoke not working for dialogs - we fixed this but in SP1...

"BTW, the forms are default created forms with MinimizeBox = False so I'm
using the 'X' to close the form."
- Forms shown via .Show have the 'x' or smart minimize. To set this to
an 'ok', use .MinimizeBox = false.

In general, you want to use Application.Run for your main form.

Hope this helps,
-Katie

This posting is provided "AS IS" with no warranties, and confers no rights.

***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp

--------------------
| Thread-Topic: How to use control 'Invoke()' from second thread - 'Catch
22'
| thread-index: AcPerTL/Dq4NP58BQ12oXmz5iSQarg==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: "=?Utf-8?B?S29obg==?=" <[email protected]>
| Subject: How to use control 'Invoke()' from second thread - 'Catch 22'
| Date: Mon, 19 Jan 2004 08:56:36 -0800
| Lines: 87
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| Path: cpmsftngxa07.phx.gbl!cpmsftngxa10.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43408
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| 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





-----Original Message-----
Hi Kohn, only need a single
call to Application.Run(), your second and subsequent forms would normally
be called from the first form as required, unless you have a main class,
using something like f2.ShowModal().
|
 
Back
Top