Kanzi中C++创建线程,子线程调用主线程update方法更新UI方式

头文件引入 

#include <kanzi/kanzi.hpp>
#include <thread>
#include <stdio.h> 
#include "windows.h"

main函数功能代码增加

using namespace kanzi;

// 主线程创建工作线程操作
printf("main thread id is %d\n", GetCurrentThreadId());
std::thread t1(&Demo::TestThread, this);
t1.detach();

void Demo::TestThread() {
    printf("child thread id is %d\n", GetCurrentThreadId());
    for (int i = 0; i < 3; i++) {
        printf("Test Thread is run: %d\n", i);
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
        // 完成工作线程切换主线程工作部分
        Domain* domain = getDomain();
        auto updateDataFuc = std::bind(&Demo::updateData, this, name, value);
        domain->getTaskDispatcher()->submit(updateDataFuc);
    }
}

// 更新Kanzi显示数据方法
void Demo::updateData(std::string name, std::string value) {
    printf("update thread id is %d\n", GetCurrentThreadId());
    // 更新KanziUI操作...
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章