我让两个线程同时调用 Javascript 的函数,其代码的第一个语句是一个警报语句,显示字符串“hello”和“world”。
我希望只有当我单击带有“hello”的警报时,才会显示警报“world”。但实际上警报“world”对话框将覆盖之前的“hello”对话框。
并且 alert("hello") 之后的代码根本没有执行。为什么会这样?
private void OnJSCallback(object sender, JSCallbackEventArgs e)
{
//Console.WriteLine("hi");
MessageBox.Show("hi");
if (count == 0)
{
Thread worker = new Thread(func =>
{
Thread.Sleep(3000);
OnReturn("CallFromNative1", "hello");
});
worker.Start();
}
if (count == 1)
{
Thread worker = new Thread(func =>
{
Thread.Sleep(3000);
OnReturn("CallFromNative2", "world");
});
worker.Start();
}
count++;
}
private void OnReturn(string call, string ret)
{
webControl.Dispatcher.BeginInvoke((ThreadStart) delegate()
{
Console.WriteLine("before call javascript");
webControl.CallJavascriptFunction("", call, new JSValue[] { new JSValue(ret)});
});
}
JavaScript 代码是:
external.JSCallback("hello", "world");
external.JSCallback("wtf", "dude");
var count = 0;
function CallFromNative1()
{
alert(arguments[0]);
var i = 0;
count = 10;
alert(count);
for(i=0; i<1000; i++)
{
count += i;
}
alert(count);
}
function CallFromNative2()
{
alert(arguments[0]);
alert(count);
}