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操作...
}

 

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