473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

"InvalidCastException 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 1839
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.EndInvoke...

What I meant was something like:

void MyCallback(IAsyncResult 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((MethodInvoker)delegate
{
// (note: this bit runs on the UI thread)
// TODO: things that impact your WebBrowser, e.g.
webBrowser1.Left += 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((MethodInvoker) {
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
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...
11
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...
4
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...
0
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...
0
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,...
6
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...
1
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...
1
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...
4
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.