aardio工作线程池

import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
btCreate={cls="button";text="创建处理线程";left=123;top=164;right=245;bottom=212;z=3};
btFinish={cls="button";text="结束处理线程";left=412;top=165;right=534;bottom=213;z=2};
btSendMsg={cls="button";text="发送任务";left=270;top=165;right=392;bottom=213;z=1}
)
/*}}*/

//线程通道
import console;
import thread.channel;

//创建通道
var chan1 = thread.channel(1);
var chan2 = thread.channel(1);

var hwdThread = 0;

winform.btCreate.oncommand = function(id, event) {
    //创建工作线程    
    if (hwdThread != 0) {
        console.log('工作线程已经创建');
        return;
    }
    
    hwdThread = thread.create(
        function(chan1, chan2) {
            
            import console;
            
            console.log('工作线程创建啦');
            
            while (true) {
                
                str <<= chan1
                if (str == 'exit') {
                    chan2 <<= 'exit';
                    console.log('工作线程结束')
                    break;
                }
                str = 'hello' + str;
                sleep(100)
                chan2 <<= str;
                
            }
            
        }, chan1, chan2
    )
    
}

winform.btSendMsg.oncommand = function(id, event) {
    if (!chan1.empty() || !chan2.empty()) {
        console.log('已有数据在待处理中');
        return;
    }
    for (i = 1; 100; 1) {
        chan1 <<= '你好'++i
        v <<= chan2;
        if (v == 'exit') break;
        console.log(v);
    }
}

winform.btFinish.oncommand = function(id, event) {
    if (hwdThread != 0) {
        chan1 <<= 'exit';
        raw.closehandle(hwdThread);
        hwdThread = 0;
        if (!chan2.empty()) {
            v <<= chan2;
        }
    }
}

winform.show();
win.loopMessage();

 

posted @ 2024-03-06 09:07  lobtao  阅读(32)  评论(0)    收藏  举报