首页 新闻 赞助 找找看

启用多线程调用webBrowsers函数报错:指定的转换无效

0
悬赏园豆:10 [待解决问题]

winform代码:

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class LoadTest : Form
{
public LoadTest()
{
InitializeComponent();
}

private void LoadTest_Load(object sender, EventArgs e)
{

this.Invoke(new Action(() =>
{
this.webBrowser1.Navigate(ConfigurationManager.AppSettings["WebBrowserPath"]);
}));
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;//跨线程访问
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(test));
thread.Start();
}
private void test()
{
this.webBrowser1.Document.InvokeScript("InvokeScriptTest", new object[] { "aaa" });//指定的转换无效
}

}

JS代码:

 

<script type="text/javascript">
function InvokeScriptTest(str) {
alert(str);
}
</script>

不用线程没有问题,就是用线程调用webBrowsers里的函数会报错

黄忠的主页 黄忠 | 初学一级 | 园豆:190
提问于:2014-06-04 16:46
< >
分享
所有回答(1)
0

你用多线程的时候没有考虑线程的同步啊,你用线程调用的时候,页面还没有加载好吧!

刘宏玺 | 园豆:14020 (专家六级) | 2014-06-04 16:53

 查了下资料 貌似要用委托解决,不知道咋用

支持(0) 反对(0) 黄忠 | 园豆:190 (初学一级) | 2014-06-04 17:05

 用DocumentCompleted事件?根本无用啊

支持(0) 反对(0) 黄忠 | 园豆:190 (初学一级) | 2014-06-04 17:07

@冷0空2气: webBrowser1_DocumentCompleted事件里面执行你的load里面的方法

支持(0) 反对(0) 刘宏玺 | 园豆:14020 (专家六级) | 2014-06-04 19:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册