我让两个线程同时调用Javascript的函数,其代码的第一个语句是一个警告语句,显示字符串“hello”& “世界”。
我预计只有当我点击“你好”的提醒时,才会显示警告“世界”。但事实上,警报“世界”对话框将涵盖之前的“你好”对话框。
并且警报(“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);
}