Delphi 线程resume 不能调用Execute

如果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了, 不知道怎么回事。不找原因啦, 就用上面这个方法啦。 如果明白的,请告知小弟,嘿嘿。