쉬면서 해놓은거 같은 코드가 temp 폴더에 있어서 옮겨놈.
해당 예제는 one thread 로 multi thread 코드로 바꾸어 메인 작업이 중단되지 않게 사용하면 되겠다.
접기
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <TlHelp32.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Log(const String &AValue)
{
Memo1->Lines->Add(AValue);
}
//---------------------------------------------------------------------------
int __fastcall ExecAndWait(const String &ACommand)
{
TStartupInfo si = {0};
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
TProcessInformation pi;
if (::CreateProcess(NULL, ACommand.c_str(), NULL, NULL, FALSE, CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL, 0, &si, &pi))
{
::SetThreadPriority(pi.hThread, THREAD_PRIORITY_IDLE);
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
::SetPriorityClass(::GetCurrentProcess(), HIGH_PRIORITY_CLASS);
::CloseHandle(pi.hProcess);
::ResumeThread(pi.hThread);
::CloseHandle(pi.hThread);
bool bFound = true;
PROCESSENTRY32 PE32;
HANDLE hSnapShot;
while (bFound == true)
{
Sleep(1);
Application->ProcessMessages();
bFound = false;
hSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapShot != HANDLE(-1))
{
try
{
PE32.dwSize = sizeof(PROCESSENTRY32);
if (::Process32First(hSnapShot, &PE32) == true)
do
{
if (PE32.th32ProcessID == pi.dwProcessId)
{
bFound = true;
break;
}
} while (::Process32Next(hSnapShot, &PE32) == true);
}
__finally
{
::CloseHandle(hSnapShot);
}
}
}
}
else
return 0;
return 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if (ExecAndWait("NotePad.exe") == false)
ShowMessage("CreateProcess 실패");
ShowMessage("완료");
}
//---------------------------------------------------------------------------
접기