C# 一段时间后取消异步操作WinRT
我试图在C# 一段时间后取消异步操作WinRT,c#,windows-runtime,async-await,C#,Windows Runtime,Async Await,我试图在WebView中使用Skulpt执行Python脚本。如果python脚本包含无限循环,则应用程序不会给出响应 从C执行Python脚本# 在JavaScript中: function evalPy(script) { try { var result = Sk.importMainWithBody("<stdin>", false, script); return Sk.builtins.repr(result).v; } c
WebView
中使用Skulpt执行Python脚本。如果python脚本包含无限循环,则应用程序不会给出响应
从C执行Python脚本#
在JavaScript中:
function evalPy(script) {
try {
var result = Sk.importMainWithBody("<stdin>", false, script);
return Sk.builtins.repr(result).v;
} catch (err) {
}
}
第二次尝试:
var op = webView.InvokeScriptAsync("evalPy", new string[1] { script });
new Task(async () =>
{
await Task.Delay(2000);
op.Cancel();
op.Close();
}).Start();
还尝试在JavaScript中设置超时
function evalPy(script) {
try {
var result = Sk.importMainWithBody("<stdin>", false, script);
setTimeout(function () { throw "Times-out"; }, 2000);
return Sk.builtins.repr(result).v;
} catch (err) {
}
}
函数求值(脚本){
试一试{
var result=Sk.importMainWithBody(“”,false,script);
setTimeout(函数(){throw“timeout”;},2000);
返回Sk.builtins.repr(result).v;
}捕捉(错误){
}
}
还使用Skulpt在Web浏览器中执行Python脚本,并在一段时间后停止执行Python脚本。我刚从Codecademy的html课程中爬出来,不知道详细信息,但javascript是一种单线程语言,我听说需要一个Web工作者来执行多线程
importScripts('./skulpt.js');
importScripts('./skulpt.min.js');
importScripts('./skulpt-stdlib.js');
// file level scope code gets executed when loaded
// Executed when the function postMessage on
//the worker object is called.
// onmessage must be global
onmessage = function(e){
var out = [];
try{
Sk.configure({output:function (t){out.push(t);}});
Sk.importMainWithBody("<stdin>",false,e.data);
}catch(e){out.push(e.toString());}
postMessage(out.join(''));
}
但是有一个缺点,当我在python代码中使用input()时,skulpt抛出一个错误,它无法找到窗口对象,因为它在工作线程中,我还没有解决方案
p、 美国。
一些测试显示下面的代码冻结了主线程(滥发postMessage是个坏主意):
SkulptWorker.js:
importScripts('./skulpt.js');
importScripts('./skulpt.min.js');
importScripts('./skulpt-stdlib.js');
// file level scope code gets executed when loaded
// Executed when the function postMessage on
//the worker object is called.
// onmessage must be global
onmessage = function(e){
try{
Sk.configure({output:function (t){postMessage(t);}});
Sk.importMainWithBody("<stdin>",false,e.data);
}catch(e){postMessage(e.toString());}
}
importScripts('./skulpt.js');
进口许可证('./skulpt.min.js');
进口许可证('./skulpt stdlib.js');
//文件级作用域代码在加载时执行
//当函数postMessage打开时执行
//调用worker对象。
//onmessage必须是全局的
onmessage=函数(e){
试一试{
configure({output:function(t){postMessage(t);}});
Sk.importMainWithBody(“”,false,e.data);
}catch(e){postMessage(e.toString());}
}
当您等待时,在执行完成之前,该方法不会返回到JavaScript。因此,无限循环将永远不会返回。当您等待两秒钟然后取消时会发生什么?当我等待两秒钟然后取消时,无限循环继续。调试器将任务状态显示为已取消,但应用程序不响应python进程将继续执行,但您的方法调用应返回。您在方法调用中还做了什么吗?是的,python脚本继续执行,但方法调用返回。不,我没有在方法调用中执行任何其他操作。创建两个任务怎么样,一个是简单的Task.Delay
,另一个是有问题的脚本。然后使用Task.WaitAny
。最后,如果任务仍在运行,请取消它并继续。
importScripts('./skulpt.js');
importScripts('./skulpt.min.js');
importScripts('./skulpt-stdlib.js');
// file level scope code gets executed when loaded
// Executed when the function postMessage on
//the worker object is called.
// onmessage must be global
onmessage = function(e){
var out = [];
try{
Sk.configure({output:function (t){out.push(t);}});
Sk.importMainWithBody("<stdin>",false,e.data);
}catch(e){out.push(e.toString());}
postMessage(out.join(''));
}
var skulptWorker = new Worker('SkulptWorker.js');
skulptWorker.onmessage = function(e){
//Writing skulpt output to console
console.log(e.data);
running = false;
}
var running = true;
skulptWorker.postMessage('print(\'hello world\')');
running = true;
skulptWorker.postMessage('while True:\n print(\'hello world\')');
setTimeout(function(){
if(running) skulptWorker.terminate();},5000);
importScripts('./skulpt.js');
importScripts('./skulpt.min.js');
importScripts('./skulpt-stdlib.js');
// file level scope code gets executed when loaded
// Executed when the function postMessage on
//the worker object is called.
// onmessage must be global
onmessage = function(e){
try{
Sk.configure({output:function (t){postMessage(t);}});
Sk.importMainWithBody("<stdin>",false,e.data);
}catch(e){postMessage(e.toString());}
}