发布 winThreadTask 扩展库, 用于创建线程并且接收返回值
By
terrorist
at 2022-05-28 • 1人收藏 • 1049人看过
import thread.command; import win.guid; namespace aaz; class winThreadTask{ ctor(){ var thCmd = ..thread.command() }; create = function(func, onEnd, ...){ var guid = tostring(..win.guid.create()); thCmd[guid] = function(...){ onEnd(...); thCmd[guid] = null; } ..thread.invoke( function(func, guid, ...){ import thread.command; thread.command.post(guid, func(...)) }, func, guid, ... ) } }
create ( 线程函数, 接收返回值的函数, 线程函数的不定个参数 )
与 thread.manage 的 create 区别:使用简单,不用执行 manage.waitClose() 才能接收返回值
与 thread.invokeAndWait 的区别:同时执行多个线程函数时不用等待耗时较长的那个函数
使用例子如下:
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio form";right=759;bottom=469) winform.add( button={cls="button";text="sleep 2000";left=37;top=64;right=156;bottom=103;z=3}; button2={cls="button";text="sleep 5000";left=41;top=118;right=153;bottom=158;z=4}; button3={cls="button";text="sleep 2000";left=226;top=65;right=345;bottom=104;z=5}; button4={cls="button";text="sleep 5000";left=226;top=119;right=338;bottom=159;z=6}; button5={cls="button";text="sleep 2000";left=422;top=65;right=541;bottom=104;z=8}; button6={cls="button";text="sleep 5000";left=422;top=119;right=534;bottom=159;z=9}; groupbox={cls="groupbox";text="winThreadTask";left=22;top=35;right=171;bottom=175;edge=1;z=1}; groupbox2={cls="groupbox";text="thread.invokeAndWait";left=211;top=36;right=366;bottom=177;edge=1;z=2}; groupbox3={cls="groupbox";text="thread.manage";left=407;top=36;right=562;bottom=177;edge=1;z=7} ) /*}}*/ import thread.manage; import aaz.winThreadTask; var task = aaz.winThreadTask() var manage = thread.manage(10) var threadFunc = function(duration){ sleep(duration) } winform.button.oncommand = function(id,event){ winform.button.disabled = true task.create( threadFunc, function(){ winform.button.disabled = false }, 2000 ) } winform.button2.oncommand = function(id,event){ winform.button2.disabled = true task.create( threadFunc, function(){ winform.button2.disabled = false }, 5000 ) } winform.button3.oncommand = function(id,event){ owner.disabled = true thread.invokeAndWait(threadFunc, 2000) owner.disabled = false } winform.button4.oncommand = function(id,event){ owner.disabled = true thread.invokeAndWait(threadFunc, 5000) owner.disabled = false } winform.button5.oncommand = function(id,event){ owner.disabled = true manage.create(threadFunc, 2000).onEnd = function(...){ winform.button5.disabled = false } manage.waitClose() } winform.button6.oncommand = function(id,event){ owner.disabled = true manage.create(threadFunc, 5000).onEnd = function(...){ winform.button6.disabled = false } manage.waitClose() } winform.show(); win.loopMessage();
4 个回复 | 最后更新于 2022-05-29
想利用这个库实现在 web.sciter 里面执行耗时函数,执行完毕后运行一个回调函数, 为什么以下代码会报错呢 ?
import win.ui; /*DSG{{*/ var winform = win.form(text="external 接口";right=1014;bottom=523) winform.add() /*}}*/ io.open() import thread.command; import web.sciter; import web.sciter.debug var wbSciter = web.sciter( winform ); wbSciter.attachEventHandler(web.sciter.debug) var thCmd = thread.command() wbSciter.external = { func = function(param, proc){ //proc(1,2) thCmd.end = function(){ proc(1,2) } thread.invoke( function(){ import thread.command thread.command.post( "end") } ) } } wbSciter.html = /** <body> <button id="my-button">调用耗时函数</button> <script> document.on("click", "#my-button", (evt, dom)=>{ external.func( "参数", function(a,b){ dom.textContent = "执行完毕" } ) }) </script> </body> **/ winform.show(); win.loopMessage();
测试出一个解决方案,要先把 proc 复制一份
var procCopy = proc.clone()
完整如下:
import win.ui; /*DSG{{*/ var winform = win.form(text="external 接口";right=1014;bottom=523) winform.add() /*}}*/ io.open() import thread.command; import web.sciter; import web.sciter.debug var wbSciter = web.sciter( winform ); wbSciter.attachEventHandler(web.sciter.debug) var thCmd = thread.command() wbSciter.external = { func = function(param, proc){ var procCopy = proc.clone() thCmd.end = function(){ procCopy() } thread.invoke( function(){ import thread.command thread.command.post( "end") } ) } } wbSciter.html = /** <body> <button id="my-button">调用耗时函数</button> <script> document.on("click", "#my-button", (evt, dom)=>{ external.func( "参数", function(a,b){ dom.textContent = "执行完毕" } ) }) </script> </body> **/ winform.show(); win.loopMessage();
登录后方可回帖
真不错,感谢分享