如何讓一個進程在執行時暫停下來,等我執行完另一個進程時再繼續執行下去呢?

xhwu3721 2003-10-21 08:57:47
一個對話框程式,我想編程控制它,讓它在被按完test按鈕後暫停一段時間,等我完成掃描動作後再繼續執行下去,如何辦到呢?
謝謝!
...全文
119 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
xhwu3721 2003-10-23
  • 打赏
  • 举报
回复
我這樣得到了線程的名柄,但為什麼停不住啊?

//枚舉並查找進程ID:

PROCESSENTRY32 p32;
HANDLE hprocess;
HANDLE pName=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
//開始查找
BOOL Next=Process32First(pName,&p32);
int i=0;
//遍歷進程
while(Next)
{
//?示?程
if((strcmpi(p32.szExeFile,"loadappi.exe"))==0) //"你要的?程");
{
//根据?程ID?取句并
hprocess=OpenProcess(PROCESS_TERMINATE,0,p32.th32ProcessID);
//好了?得你要的句柄了。
AfxMessageBox("have found the loadappi.exe process");
break;
}
//??查找
Next=Process32Next(pName,&p32);
i++;
}
SuspendThread(hprocess);

CloseHandle(pName);

AfxMessageBox("SuspendThread ok?");
awant2k 2003-10-22
  • 打赏
  • 举报
回复
ExitProcess 本来就是结束自己的进程和它所有的线程
你用 TerminateProcess

ExitProcess
The ExitProcess function ends a process and all its threads.

VOID ExitProcess(
UINT uExitCode // exit code for all threads
);

Parameters
uExitCode
Specifies the exit code for the process, and for all threads that are terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.



TerminateProcess
The TerminateProcess function terminates the specified process and all of its threads.

BOOL TerminateProcess(
HANDLE hProcess, // handle to the process
UINT uExitCode // exit code for the process
);

Parameters
hProcess
Handle to the process to terminate.
Windows NT: The handle must have PROCESS_TERMINATE access.

uExitCode
Specifies the exit code for the process and for all threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

awant2k 2003-10-22
  • 打赏
  • 举报
回复
用SendNotifyMessage 试试


SendNotifyMessage
The SendNotifyMessage function sends the specified message to a window. If the window was created by the calling thread, SendNotifyMessage calls the window procedure for the window and does not return until the window procedure has processed the message. If the window was created by a different thread, SendNotifyMessage passes the message to the window procedure and returns immediately; it does not wait for the window procedure to finish processing the message.

BOOL SendNotifyMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to send
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);


xhwu3721 2003-10-21
  • 打赏
  • 举报
回复
謝謝各位,已經ok了,但是挂起之後,執行完成之後,我想終止被控程序的進程,
我用ExitProcess(dw);但是終止的不是被控的進程,卻是我自己寫的程序,不知為什什 ?
STARTUPINFO si={sizeof(STARTUPINFO)};
PROCESS_INFORMATION pi;
CreateProcess(0,"E:\\MyDebugProg\\DllTech\\loadioApp\\Debug\\loadappi.exe",0,0,0,0,0,0,&si,&pi);

DWORD dw;
GetExitCodeProcess(pi.hProcess,&dw);
if(dw==STILL_ACTIVE)
{
AfxMessageBox("the process still active");
}
else
{
AfxMessageBox("the process end");
}
ExitProcess(dw);
larryvc 2003-10-21
  • 打赏
  • 举报
回复
当要让一个进程暂时停止时可以用SuspendThread( HANDLE hThread ) ,这是线程的全局函数会停止,需要让他重新起动时可用ResumeThread( HANDLE hThread ) ,这是全局函数会重新开始
wilehorse 2003-10-21
  • 打赏
  • 举报
回复
SuspendThread();
ResumeThread();
xhwu3721 2003-10-21
  • 打赏
  • 举报
回复
to: awant2k(一点通)大俠, 好象剛好說反了,我是指那個被控制的程序不是我寫的,它是一個對話框程式,有一個test按鈕,我的目的是想讓它在被按了test之後,就被暫停,而不是整個退出來,我自己寫的程式能偵測到其test按鈕被按,然後我的程式彈出一個對話框完成掃描後,再去讓那個進行繼續執行下去,這樣能辦到嗎?
awant2k 2003-10-21
  • 打赏
  • 举报
回复
把 扫描动作 作为一个进程/线程 启动,如 CreateProcess 创建进程/线程
它的第10个(最后)参数为LPPROCESS_INFORMATION lpProcessInformation,

要等待扫描可以用 WaitForSingleObject
要暂停扫描可以用 SuspendThread
要恢复扫描可以用 ResumeThread



附MSDN:
1) BOOL CreateProcess(
LPCTSTR lpApplicationName,
// pointer to name of executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // process security attributes
LPSECURITY_ATTRIBUTES lpThreadAttributes, // thread security attributes
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current directory name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);

2)
DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds
);

3) HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
DWORD dwStackSize, // initial thread stack size
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to receive thread ID
);

4) DWORD SuspendThread( HANDLE hThread ) // handle to the thread);


5) DWORD ResumeThread( HANDLE hThread ) // identifies thread to restart);
akiko 2003-10-21
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2363/2363816.xml?temp=.2944605
efei4000 2003-10-21
  • 打赏
  • 举报
回复
挂起和恢复不就可以吗?

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧