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?
My "create" button click slot
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;
}
My "Start" button click slot
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();
}
}
My thread function
DWORD WINAPI MainWindow::ThreadFunc1(LPVOID lpParams)
{
for(int i = 0; i < 100; i++)
{
ui->progressBar->setValue(i);
sleep(500);
}
return 0;
}
Other 2 thread functions are the same, only using different progress bar.