Hi Brad,
Welcome to the MSDN newsgroup.
From your description, I understand you've upgraded a VB6 application to
VB.NET(.net framework 1.1 or 2.0?), this application use an activeX
control, and it is hosted through COM interop in the current VB.NET winform
application. The activeX control will raise some events and send
notifications, however, you found that in the event handler, if you try
updating the controls on the main form, you'll get
"System.InvalidOperationException" exception while it works well send info
into Console buffer, correct?
Based on my experience, this is likely a thread cooperate issue as you've
expected. Is the code that updateing the controls on form put in your
activeX control's event handler and have you checked that whether the event
handler code is executed in a separate thread from the windows
application's main UI thread? The potential cause is that the code which
updating the controls on the form is executed on a worker thread other than
the main UI thread, and for form controls(they're win32 controls which are
created on the main UI thread) which can only be manipulated under the UI
thread in which they're created. Therefore, we can not directly reference
and manipulate them in a separate thread, this restriction is enforced in
..net framework 2.0.
For the scenario we want to accessing and updating the controls on windows
form in a non-UI workerthread, we can use the following methods(on Control
instance) to marshal a function call into the main UI thread of that
control:
#Control.Invoke Method
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.invoke
..aspx
#Control.BeginInvoke Method
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.begini
nvoke.aspx
e.g. Suppose we have the following function which will update the controls
on form:
private void UpdateControlsOnForm(object sender, EventArgs e) {
TextBox1.Text = ........;
.......................
}
In a non-UI worker thread, we can use the below code to marshal the
function call onto the control's UI thread:
//myControl is a control on the form and created on the main UI thread
myControl.Invoke(new EventHandler(UpdateControlsOnForm));
In addition, here are some good web articles and msdn reference describing
this problem:
#Updating Controls From Worker Threads
http://codemilitia.com/blogs/tobin.titus/archive/2005/04/10/12.aspx
#How to: Manipulate Controls from Threads
http://msdn2.microsoft.com/en-us/library/757y83z4.aspx
#WinForms UI Thread Invokes: An In-Depth Review of
Invoke/BeginInvoke/InvokeRequred
http://weblogs.asp.net/justin_rogers/articles/126345.aspx
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial
response from the community or a Microsoft Support Engineer within 1
business day is
acceptable. Please note that each follow up response may take approximately
2 business days
as the support professional working with you may need further investigation
to reach the
most efficient resolution. The offering is not appropriate for situations
that require
urgent, real-time or phone-based interactions or complex project analysis
and dump analysis
issues. Issues of this nature are best handled working with a dedicated
Microsoft Support
Engineer by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.