C++中Qt中使用WINAPI线程的问题 [英] Problem with using WINAPI threads in Qt in C++

查看:76
本文介绍了C++中Qt中使用WINAPI线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被指派创建一个程序,该程序处理三个 WINAPI 线程并反映进度条中的工作流程.我决定将 Qt Widgets 用于这些目的.我使用 CREATE_SUSPENDED 创建标志创建处于挂起状态的线程,然后在单击按钮时使用 ResumeThread 函数恢复它.当我单击它时,程序因未处理的异常 win32 而崩溃.为什么会发生这种情况?

I was assigned to create a program that handles three WINAPI threads and reflects the workflow of those in progress bars. I decided to use Qt Widgets for these purposes. I create those threads in suspended state, using CREATE_SUSPENDED creation flag, then i resume it with ResumeThread function upon clicking the button. When I click it, the program crashes with an unhandled exception win32. Why could this happen?

我的创建"按钮点击槽

void MainWindow::on_pb_create_clicked()
{
    hThread[0] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc1, NULL, CREATE_SUSPENDED, &thID[0]);
    hThread[1] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc2, NULL, CREATE_SUSPENDED, &thID[1]);
    hThread[2] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&ThreadFunc3, NULL, CREATE_SUSPENDED, &thID[2]);
    threadsCreated = true;
}

我的开始"按钮点击槽

void MainWindow::on_pb_start_clicked()
{
    if(threadsCreated)
    {
       for(int i = 0; i = 2; i++)
       {
           ResumeThread(hThread[i]);
       }
    }
    else
    {
        QMessageBox msg;
        msg.setWindowTitle("Error");
        msg.setText("Threads are not created");
        msg.exec();
    }
}

我的线程函数

DWORD WINAPI MainWindow::ThreadFunc1(LPVOID lpParams)
{
    for(int i = 0; i < 100; i++)
    {
        ui->progressBar->setValue(i);
        sleep(500);
    }
    return 0;
}

其他2个线程功能相同,只是进度条不同.

Other 2 thread functions are the same, only using different progress bar.

推荐答案

看来您正在传递一个成员函数指针作为应该执行的函数.请记住,除非静态成员函数,否则您不能在没有执行"函数调用的对象实例的情况下调用成员函数.

It seems you are passing a member function pointer as the function that should be executed. Remember that you can't call a member function without an object instance that "performs" the function call, except for static member functions.

请参阅此线程以获取解决方法:您如何将 CreateThread 用于属于类成员的函数?

See this thread for a workaround: How do you use CreateThread for functions which are class members?

底线是您调用一个静态成员函数,将应该执行某些成员函数调用的对象(在本例中为您的 MainWindow 实例)传递给它,然后该实例执行调用.

Bottom line is you call a static member function, pass it the object that should perform some member function call (your MainWindow instance in this case), then the instance performs the call.

请参阅返回值"下的 CreateThread 的 Microsoft 文档:

See the Microsoft Documentation for CreateThread under "Return value":

请注意,即使 lpStartAddress 指向数据、代码或不可访问,CreateThread 也可能成功.如果线程运行时起始地址无效,则发生异常,线程终止.由于起始地址无效而终止的线程将作为线程进程的错误退出来处理.

Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process.

https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread

这篇关于C++中Qt中使用WINAPI线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆