在 C# Windows Form WebBrowser 中运行 Javacript 代码。需要它能够运行长时间循环而不会卡住表单 UI。有什么想法吗?或者其他方式运行JS来实现这个
代码:
[ComVisible(true)]
public void ExecuteJS(WebBrowser wb)
{
// Create the webpage, testing a long loop to check UI stuck or not
wb.DocumentText = @"<html>
<head>
<title>Test</title>
</head>
<body>
<script>
function TestInvoke() {
while (true) {
window.external.AnotherMethod('Hello');
wait(2500);
}
}
function wait(ms){
var start = new Date().getTime();
var end = start;
while(end < start + ms) {
end = new Date().getTime();
}
}
</script>
</body>
</html>";
}
}
调用(在一个线程上,它仍然使 UI 卡住):
private void BottingThread()
{
//run the js script
this.Invoke(new Action(() => { webBrowser1.Document.InvokeScript("TestInvoke").ToString(); }));
}