473,796 Members | 2,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AsyncCallback method accessing WebBrowser control causes exception

Hi,

We have an app which uses an AsyncCallback method to handle the return
from a COM call.

In this callback, if we try to do anything with a WebBrowser control,
we get the following exception:

"InvalidCastExc eption was unhandled
Specified cast is not valid"

If we remove all code which tries to access the WebBrowser, the
exception doesn't occur.

Has anyone got any idea why this would happen? We can access any
other controls in our app and no exception is thrown.

Any help much appreciated.

Artie
Mar 5 '08 #1
3 1860
First-off; have you used Invoke/BeginInvoke to get back to the UI
thread before touching the controls?

Otherwise, I'd guess that you simply have a bug - i.e. you are
genuinely doing an invalid cast, perhaps picking up the wrong element
from a DHTML query?

Marc
Mar 5 '08 #2
Ah - confusion due to the very confusing Delegate.[Begin]Invoke vs
Control.[Begin]Invoke; I meant the latter, which pushes work back to
the UI thread. Your EndInvoke usage sounds like Delegate.EndInv oke...

What I meant was something like:

void MyCallback(IAsy ncResult result)
{
// (note: this bit runs on a worker thread)
// TODO: usual stuff to get the answer from result

// now jump to UI thread before touching controls
this.Invoke((Me thodInvoker)del egate
{
// (note: this bit runs on the UI thread)
// TODO: things that impact your WebBrowser, e.g.
webBrowser1.Lef t += 5;
});
}
Mar 5 '08 #3
I don't pretend that it is an answer to "why", but perhaps one good thing
about this approach is that it avoids accidental and overly inefficient
code - i.e.

x.Foo = 123;
// ... lots more, perhaps involving a loop
x.Bar = "abc";

If each of those lines (executed on a worker thread) silently did an
Invoke() behind your back, you could get some *seriously* poor performance.
However, wrap that up a bit (for not much extra work) and it should perform
OK:

x.Invoke((Metho dInvoker) {
x.Foo = 123;
// ... lots more, perhaps involving a loop
x.Bar = "abc";
});

I suspect that this is more by accident than by intent, however...

Marc
Mar 6 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2431
by: Craig Vermeer | last post by:
Hi All, I noticed an interesting issue the other day, and thought someone here might be able to shed some light on things. I have a situation where I'm using asynchronous delegates, and an exception is getting thrown from within the AsyncCallback on the delegate. For some reason, this causes the AsyncCallback delegate to be executed twice. Now, I understand how to stop the situation from happening. However, what I didn't know was...
11
3790
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and noticed that if I did this, it worked (please ignore the lack of try ... catch blocks, because I cut down the code to be more brief - I have a try...catch surround the AcquireWriterLock method): ---------- myLock.AcquireWriterLock(1000);...
4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
0
2267
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or updating the code-behind dll) accessing any aspx page in the application causes the application to run for the first time. Some of the initialization involves reading and writing some text and xml files using simple streamreader and streamwriter...
0
3480
by: Jim Hubbard | last post by:
How would I implement the IDispatch interface to handle the following in VB.Net <BEGIN> Controlling Download and Execution The WebBrowser Control gives you control over what it downloads, displays, and executes. To gain this control, you need to implement your host's IDispatch so it handles DISPID_AMBIENT_DLCONTROL. When the WebBrowser Control is instantiated, it will call your IDispatch::Invoke with this ID. Set pvarResult to a...
6
4834
by: Shane Story | last post by:
I am trying to add the Microsoft Web Browseractive X control to a vb.net form. I always get an error on the EndInit line. Parts of the errors says: "Unknown error" "First Chance exception"
1
3307
by: L. Chernov | last post by:
Hello, I am trying to work with .Net 2005 WebBrowser object, and put it on a "Windows control library" (embedded in a user control class) and then I am executing it from an ASP.Net webform(with IE6) with the following object tag: <OBJECT id="ModPkiObj" height="550" width="800" classid="http:ModPkiNew.dll#Mod.Online.Client.ModPkiNew.SignForm" name="ModPkiObj" VIEWASTEXT>
1
4098
by: Sin Jeong-hun | last post by:
Is it possible to modify the HTML or the script of a WebBrowser control in DocumentCompleted event? I tried to modify it but that caused DocumentCompleted again so the program fell in an endless loop. The reason I'm trying to do so is to remove some specific scripts like "alert()" because it locks up the WebBrowser control. When there's an alert box is open, calling Navigate causes an exception. I need other scripts to be executed so I...
4
4481
by: kimiraikkonen | last post by:
Hi, Just to test, i placed a simple webbrowser to login a site, this site has no problems and has same error with IE6 SP2 but it's suppressed as well and no problem with Firefox browser, but using webbrowser it shows scripting errors: There are a lot more sites that encounters scripting problems though they work just fine in Firefox, a sample: http://www.anaivanovic.com/forum/login.php
0
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.