HighTechTalks DotNet Forums  

GUI Threading and Cross Threaded Exception Question

Visual Studio.net (General) microsoft.public.vsnet.general


Discuss GUI Threading and Cross Threaded Exception Question in the Visual Studio.net (General) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
georgejetson
 
Posts: n/a

Default GUI Threading and Cross Threaded Exception Question - 06-16-2009 , 03:11 PM






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

Reply With Quote
  #2  
Old   
john
 
Posts: n/a

Default Re: GUI Threading and Cross Threaded Exception Question - 06-17-2009 , 04:02 AM






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 (AT) nospam (DOT) nospam> wrote

Quote:
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

Reply With Quote
  #3  
Old   
Jialiang Ge [MSFT]
 
Posts: n/a

Default Re: GUI Threading and Cross Threaded Exception Question - 06-25-2009 , 10:02 PM



Hello georgejetson

I agree with John. Does John's suggestion help you? If you have any
questions or concerns, please feel free to post here.

Regards,
Jialiang Ge
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg (AT) microsoft (DOT) com.

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

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.