. Net calls JS: the reason why the "specified conversion is invalid" exception is thrown by the webbrowser. Document. invokescript method.

Source: Internet
Author: User

An inexplicable exception message...

 

Use this function to call the JS script in the webbrowser control:

Webbrowser. Document. invokescript ("function name", parameter list );

However, a very difficult problem encountered in the afternoon is that such an inexplicable exception will be thrown during the call:

System. invalidcastexception not handled
Message = "the specified conversion is invalid. "
Source = "system. Windows. Forms"
Stacktrace:
In system. Windows. Forms. unsafenativemethods. ihtmldocument2.getlocation ()
In system. Windows. Forms. webbrowser. get_document ()
In ipmessagernet. UI. Controls. Chat. ieview. ietabpage. commander_textmessagereceived (Object sender, messageeventargs E)
In system. eventhandler '1. Invoke (Object sender, teventargs E)
Innerexception:

...... Inexplicable ~ That's amazing.

It was originally with a parameter. It was thought that there was a problem with the parameter. It was found that the type and number of parameters were incorrect and depressing. Later, it was found that no parameters were the same and incorrect.

After more than half an hour of fighting, I finally found the cause:Called across threads.

Simply put, when the above function is called, it is not the main interface thread (because it is a function callback), and the result reports such an inexplicable error.

Solution: Use the delegate to put the function in the main thread for calling.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.