C#" InvalidCastException的"试图从TimerCallback访问WebBrowser控件时 [英] C# "InvalidCastException" when trying to access webbrowser control from TimerCallback

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

问题描述

基本上我有同样的问题,因为该用户:
<一href=\"http://stackoverflow.com/questions/5717934/how-to-check-for-trackbar-sliding-with-mouse-hold-and-release\">How检查的TrackBar鼠标滑动保持和释放
我解决了这个使用所提供的第一个解决方案。然而,当计时器被调用时,我想打电话给InvokeScript上WebBrowser控件。 InvokeScript运行没有错误,但JavaScript函数永远不会被调用。当我把这个脚本就像一个按钮点击事件处理程序,函数正确调用。

Basically I have the same problem as this user: How to check for TrackBar sliding with mouse hold and release I fixed this using the first solution provided. However, when the timer is called, I want to call InvokeScript on a webbrowser control. InvokeScript runs without an error, but the javascript function is never called. When I call this script from like a button clicked event handler, the function is called properly.

我发现,当我尝试从WebBrowser控件访问属性(比如MessageBox.Show(webBrowser1.DocumentText),这将引发一个InvalidCastException的。

I found out that when I try to access properties from the webbrowser control (like MessageBox.Show(webBrowser1.DocumentText), this throws a InvalidCastException.

// in constructor:
webBrowser1.AllowWebBrowserDrop = false;
webBrowser1.IsWebBrowserContextMenuEnabled = false;
webBrowser1.WebBrowserShortcutsEnabled = false;
webBrowser1.ObjectForScripting = this;
timer = new System.Threading.Timer(this.TimerElapsed);     

private void trackBar2_ValueChanged(object sender, EventArgs e)
{
        timer.Change(500, -1);
}
private void TimerElapsed(object state)
{
    this.webBrowser1.InvokeScript("jmp_end");
    MessageBox.Show(this.webBrowser1.DocumentText);
    timerRunning = false;
}
private void TimerElapsed(object state)
{
    WebBrowser brw = getBrowser();
    brw.Document.InvokeScript("jmpend");
    MessageBox.Show(brw.DocumentText);
    timerRunning = false;
}

有谁知道我在做什么错在这里?还是有另一种方式来获得同样的结果?

Does anyone know what I am doing wrong here? Or is there another way to get the same result?

在约InvokeRequired的意见,这听起来完全像我所需要的。但我不能得到它的工作。这是我从样品code从制作的 C#System.InvalidCastException

After comments about InvokeRequired, this sounds exactly like what I need.. But I can't get it working.. This is what I made from the sample code from C# System.InvalidCastException

public delegate WebBrowser getBrowserHandler();
public WebBrowser getBrowser()
{
    if (InvokeRequired)
    {
        return Invoke(new getBrowserHandler(getBrowser)) as WebBrowser;
    }
    else
    {
        return webBrowser1;
    }
}

private void TimerElapsed(object state)
{
    WebBrowser brw = getBrowser();
    brw.Document.InvokeScript("jmpend");
    MessageBox.Show(brw.DocumentText);
    timerRunning = false;
}

有什么我错过了吗?

What have I missed here?

推荐答案

主叫方(定时器)是比对照创建在不同的线程。

The caller (the timer) is on a different thread than the control was created on.

请参阅 Control.InvokeRequired地产

样code应该解决您的问题张贴在这个问题: C#System.InvalidCastException

Sample code that should address your issue is posted on this question: C# System.InvalidCastException

这篇关于C#&QUOT; InvalidCastException的&QUOT;试图从TimerCallback访问WebBrowser控件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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