Re: GUI Threading and Cross Threaded Exception Question
- From: "john" <john@xxxxx>
- Date: Wed, 17 Jun 2009 17:02:12 +0800
Hi georgejetson
You can protect against this case by also checking the value of IsHandleCreated when InvokeRequired returns false on a background thread. If the control handle has not yet been created, you must wait until it has been created before calling Invoke or BeginInvoke. Typically, this happens only if a background thread is created in the constructor of the primary form for the application (as in Application.Run(new MainForm()), before the form has been shown or Application.Run has been called.
Regards,
John
"georgejetson" <samiam@xxxxxxxxxxxxx> wrote in message news:61117DA3-C572-4844-BC16-9EFAAD9E4C9C@xxxxxxxxxxxxxxxx
Hi,
VB.Net 2005
Windows XP Pro
Is this legal? It works...something doesn't seem quite right...???
Problem outlined below (at end)
'This is the main application form...main thread
public sub new()
InitializeComponent()
'This is another form I want to run on its own msg pump
dim myForm as new frmSomeForm() 'Initialize Component is called here
myForm.myProperty = 10 'No big deal
Dim t As New Thread(New ParameterizedThreadStart(AddressOf startUIThread))
t.SetApartmentState(ApartmentState.STA)
t.Start(myForm)
end sub
<STAThread()> _
Private Sub startUIThread(ByVal theForm As Object)
Try
Application.Run(CType(theForm, Form))
Catch ex As Exception
log.Error(ex.ToString, ex.InnerException)
End Try
End Sub
My problem (in frmSomeForm):
My log file indicates InvokeRequired is FALSE, but when I access the combo
box on this form, I'm getting a CROSS THREADED EXCEPTION...?
Private Sub mvarDataSupplier_initialized() Handles
mvarDataSupplier.initialized
Try
If (InvokeRequired) Then
log.Debug("InvokeRequired")
Dim d As New mvarDataSupplier_initializedDelegate(AddressOf
mvarDataSupplier_initialized)
Invoke(d, Nothing)
Exit Sub
End If
With Me.cboGroups
.Items.Clear() 'cross threaded exception here
End With
End Sub
--
thanks in advance,
georgejetson
.
- Follow-Ups:
- Re: GUI Threading and Cross Threaded Exception Question
- From: "Jialiang Ge [MSFT]"
- Re: GUI Threading and Cross Threaded Exception Question
- References:
- GUI Threading and Cross Threaded Exception Question
- From: georgejetson
- GUI Threading and Cross Threaded Exception Question
- Prev by Date: Re: Computing Elapsed Time
- Next by Date: RE: Database Deployment ?
- Previous by thread: GUI Threading and Cross Threaded Exception Question
- Next by thread: Re: GUI Threading and Cross Threaded Exception Question
- Index(es):
Relevant Pages
|