Re: GUI Threading and Cross Threaded Exception Question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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

.



Relevant Pages

  • Re: Loading of database in Userform_initialize subroutine
    ... how your platform should look like, with the "Private Sub document_Close" ... If the userform is unloaded (maybe by an Unload Me statement in a button handler ... Dim db As DAO.Database ... I invoke the userform by the following simple code: ...
    (microsoft.public.word.vba.general)
  • RE: Help Thread pooling ???
    ... my treeView is ona main form, the procedure I called in the thread pool is ... Or should I remove the Invoke outside of the threadpool? ... > Private Sub AddToTreeView ...
    (microsoft.public.dotnet.general)
  • Re: Threading & raising event question
    ... I don't want to use the Invoke method at all. ... Public Event Found(ByVal sender As Object, ByVal e As FoundEventArgs) ... Protected Friend Sub OnFound ...
    (microsoft.public.dotnet.languages.vb)
  • Re: threading question
    ... The control is created in the class constructor (sub New()), ... FireTest calls InvokeRequired and Invoke on the control to ensure that the ... 'It can be modified using the Windows Form Designer. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: BackGroundWorker.ProgressChanged "Cross-thread operation not valid" at 2nd/3th/... progress
    ... Control.Invoke worked just fine for me in a VS .NET 2003 add-in. ... This is my new code with Invoke, ... Private Sub bgwInfoOutlook_ProgressChanged(ByVal sender As Object, ... Control.Invoke method to run the UI update code on the UI thread. ...
    (microsoft.public.dotnet.framework)