如果Resume不能喚起線程,請試試如下的函數,試試。
GetExitCodeThread(ThreadHandle,ExitCode)
來取得ExitCode,如果ExitCode=STILL_ACTIVE
表示線程還存在,對於存在的線程,則可以使用ResumeThread(ThreadHandle)來喚醒線程,如果線程仍然處於運行狀態,則會返回錯誤具體的自己去看相關的API的返回值。
本人在實際應用中還是沒能夠解決上面問題,只好委曲找到另一種解決方法。
procedure TfrmDataExchangePlatformMain.ActionStartServiceExecute(
Sender: TObject);
begin
StatusBar1.Panels[1].Text := '當前操作: ' + ActionStartService.Caption;
Screen.Cursor := crHourGlass;
try
FExecfinish := True;
Timer_Service.Enabled := True;
//MyThread.Resume;
finally
Screen.Cursor := crDefault;
end;
end;
{
procedure TfrmDataExchangePlatformMain.Timer_ServiceTimer(Sender: TObject);
var
ExitCode: DWORD;
begin
//if MyThread.MyThreadExecfinish then
//MyThread.Execute;
MyThread.Resume;
GetExitCodeThread(MyThread.Handle,ExitCode);
if not MyThread.Suspended then
begin
ShowMessage('可以運行');
end;
if ExitCode=STILL_ACTIVE then
begin
ShowMessage('可以運行');
end;
end;
}
procedure TfrmDataExchangePlatformMain.Timer_ServiceTimer(Sender: TObject);
begin
MyThread := TMyThread.CreateEx(False);
MyThread.ExeTimer := Timer_Service;
MyThread.MemoLogStrings := cxMemo_Log.Lines;
MyThread.LogStrings := cxMemo_Log.Lines;;
MyThread.MyThreadExecfinish := True;
MyThread.HTTPRIOLeExp := DMConn.HTTPRIOLeExp;
MyThread.HTTPRIONC := DMConn.HTTPRIONC;
end;
{ TMyThread }
procedure TMyThread.Execute;
begin
inherited;
FreeOnTerminate := True; {這可以讓線程執行完畢后隨即釋放}
if MyThreadExecfinish then
ExecTimer;
end;
function TMyThread.ExecTimer: Boolean;
begin
Result := False ;
FMyThreadExecfinish := False;
Screen.Cursor := crHourGlass;
CoInitialize(nil);
try
FExeTimer.Enabled := False;
TranspondClientBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
Sleep(1000);
TranspondPersonBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
Sleep(1000);
TranspondDeptBaseData_factory1(HTTPRIOLeExp, HTTPRIONC, LogStrings);
//readLogStrings;
//SaveLogFile;
finally
Result := True;
FMyThreadExecfinish := True;
CoUninitialize;
FExeTimer.Enabled := True;
Screen.Cursor := crDefault;
//Suspended := True;
end;
end;
遇到此問題的,不妨這么試試吧, 其實上面找到了原因,但是找不到解決方法, 上面應該是線程被停止了,不存在了, 但是就是不知道在哪被停止的, 因為執行兩次resume的時候 exitcode和STILL_ACTIVE還是一樣, 但是就是在以后就為0了, 不知道怎么回事。不找原因啦, 就用上面這個方法啦。 如果明白的,請告知小弟,嘿嘿。
