结束进程
结束进程
在ThreadDialog中构建了两个进程,threadA和threadB。
在关闭ThreadDialog后,这两个进程都需要结束
代码如下:
void ThreadDialog::closeEvent(QCloseEvent *event){
threadA.terminate();
threadB.terminate();
threadA.wait();
threadB.wait();
event->accept();
}
结束任务管理器中正在运行的exe文件
方式一:
头文件:
#include <QMainWindow>
#include<QThread>
#include<iostream>
#include<QString>
#include<QFileDialog>
#include<QMessageBox>
#include<opencv/cv.h>
#include<QTextCodec>
#include<QFont>
#include<opencv2/opencv.hpp>
#include <QApplication>
#include <QProcess>
#include <windef.h>
#include <tlhelp32.h>
main.cpp
#include "threaddialog.h"
void killProcess(QString ProcessName)
{
bool result = false;
QString str1;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0) ;
PROCESSENTRY32 pInfo;
pInfo.dwSize = sizeof(pInfo);
Process32First(hSnapShot, &pInfo);
do
{
str1 = (QString::fromUtf16(reinterpret_cast<const unsigned short *>(pInfo.szExeFile)));
if (str1 == ProcessName)
{
result = true;
QString cmd;
cmd = QString("taskkill /F /PID %1 /T").arg(pInfo.th32ProcessID);
system(cmd.toLatin1());
}
} while(Process32Next(hSnapShot, &pInfo) );
cout<<result<<endl;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadDialog *threaddialog = new ThreadDialog;
threaddialog->exec();
killProcess("multiThread");
return a.exec();
}
方式二
main.cpp
#include "threaddialog.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ThreadDialog *threaddialog = new ThreadDialog;
threaddialog->exec();
//用于结束线程。如果multiThread.EXE在任务管理器中未关闭,便结束该程序
QProcess process;
process.execute("TASKKILL /IM multiThread.EXE /F");
process.close();
return a.exec();
}