Using com object in .NET

B

Brad Coble

I have an activex control that is used in an old app originally written in
VB6. It works good. I updated the VB6 app to VB.net. Got it working, sort
of.

The activex control uses withevents and fires an event when there is
incoming message traffic. I need to capture that data. The event fires
sometimes. I know it should be firing more often because i run the vb6 app
and watch the traffic.

When I look at the debuger output I see a lot of this "A first chance
exception of type 'System.InvalidOperationException' occurred in
System.Windows.Forms.dll".

In addition it doesnt act like I intend. I can push a message out to the
console but if i try to update a form control it doesnt work.

I want to be able to use this activex control in C# without the withevents.

Also, The activex control is a an exe not a dll. I am suspecting a thread
problem here but not sure what to do next.

Thanks for any help!!


Brad
 
S

Steven Cheng[MSFT]

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.
 
S

Steven Cheng[MSFT]

Thanks for your response Brad,

Glad that the information is of assistance. Please feel free to let me know
if you've got any progress or need any further help on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Steven Cheng[MSFT]

Thanks for your response Brad,

Glad that the information is of assistance. Please feel free to let me know
if you've got any progress or need any further help on this.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top