C#InvalidCastException的 [英] c# InvalidCastException

查看:108
本文介绍了C#InvalidCastException的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用这个code:

I try to use this code :

webBrowser.Document.GetElementById("login").SetAttribute("value", "user");

这工作的伟大,但不是当我用它在一个新的线程。我得到一个InvalidCastException。我该怎么办?

It work great but not when i use it in a new thread. I get an InvalidCastException. What can I do ?

推荐答案

这应该工作:

delegate void ActionExecutorOnUI(ref HtmlElement a, string b, string c);
private void SetValueOnHtmlElementOnUIThread(this HtmlElement onElement, string propToChange, string valueGiven, WebBrowser linkToWebBrowser)
        {
            if (linkToWebBrowser.InvokeRequired)
            {
                ActionExecutorOnUI d = new ActionExecutorOnUI(SetValueOnHtmlElementOnUIThread);
                linkToWebBrowser.Invoke(d, new object[] { });
            }
            else
                SetValueOnHtmlElementOnUIThread(ref onElement, propToChange, valueGiven);

        }

private void SetValueOnHtmlElementOnUIThread(ref HtmlElement onElement, string propToChange, string valueGiven)
        {
            onElement.SetAttribute("value", "user"); 
        }

这篇关于C#InvalidCastException的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆