H
hgptm
Unregistered / Unconfirmed
GUEST, unregistred user!
var gMyThread : TMyThread;
//第一次調用,系統啟動的瞬間
gMyThread :=TMyThread.Create(true,5);
gMyThread.Resume;
//系統啟動後,每30秒定時器調用.(問題出在這裡:線程碼錯誤.)
if gMyThread.ThreadStatus=tsSuspended then
begin
gMyThread.Resume;
// ThreadStatus:=tsWaiting;
end else
begin
gMyThread.Suspend;
// ThreadStatus:=tsSuspended;
end;
//線程單元
unit TaskThreadEx;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
registry,inifiles,Db, ADODB,DBGrids,ExtCtrls,Grids,ComCtrls,sh4;
type
TSyncObjMethod = (smNull,smNoSync,smMutex,smSemaphore);
TThreadStatus = (tsRunning,tsSuspended,tsWaiting,tsTerminated);
(**************************************************************************)
TMyThread = class (TThread)
private
FPriorityIdx: integer;
procedure CopyDataFromServerPc;
protected
public
MyMutexHandle : THandle;
// 互斥量
ThreadStatus : TThreadStatus;
constructor Create (Suspended : boolean;PriorityIdx: integer);
procedure MyThreadExit(sender : TObject);
procedure Execute;
override;
procedure Suspend;
procedure terminate;
procedure Resume;
end;
(**************************************************************************)
implementation
uses Global, Main;
// TMyThread 創建
constructor TMyThread.Create(Suspended : boolean;PriorityIdx: integer);
begin
inherited create(Suspended);
if Suspended then
ThreadStatus:=tsSuspended
else
ThreadStatus:=tsWaiting;
//ThreadStatus:=tsRunning;
MyMutexHandle:=CreateMutex(nil,false,nil);
FPriorityIdx:=PriorityIdx;
Priority:=TThreadPriority(FPriorityIdx);
//線程級別
end;
procedure TMyThread.Execute;
begin
FreeOnTerminate:=true;
OnTerminate:=MyThreadExit;
if WaitForSingleObject(MyMutexHandle,INFINITE)=WAIT_OBJECT_0 then
begin
//Synchronize(ProcessBroadorMultiDataPacket);
Synchronize(CopyDataFromServerPc);
ThreadStatus:=tsSuspended;
end;
ReleaseMutex(MyMutexHandle);
end;
procedure TMyThread.MyThreadExit;
begin
//退出時的動作
closeHandle(MyMutexHandle);
end;
procedure TMyThread.Resume;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
procedure TMyThread.Suspend;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
procedure TMyThread.terminate;
begin
inherited;
ThreadStatus:=tsTerminated;
end;
procedure TMyThread.CopyDataFromServerPc;
var i,j: integer;
tmpstr,str: string;
buff: array[0..6000] of char;
begin
try
//....
//...
//...
Finally
//
end;
end;
end.
//第一次調用,系統啟動的瞬間
gMyThread :=TMyThread.Create(true,5);
gMyThread.Resume;
//系統啟動後,每30秒定時器調用.(問題出在這裡:線程碼錯誤.)
if gMyThread.ThreadStatus=tsSuspended then
begin
gMyThread.Resume;
// ThreadStatus:=tsWaiting;
end else
begin
gMyThread.Suspend;
// ThreadStatus:=tsSuspended;
end;
//線程單元
unit TaskThreadEx;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
registry,inifiles,Db, ADODB,DBGrids,ExtCtrls,Grids,ComCtrls,sh4;
type
TSyncObjMethod = (smNull,smNoSync,smMutex,smSemaphore);
TThreadStatus = (tsRunning,tsSuspended,tsWaiting,tsTerminated);
(**************************************************************************)
TMyThread = class (TThread)
private
FPriorityIdx: integer;
procedure CopyDataFromServerPc;
protected
public
MyMutexHandle : THandle;
// 互斥量
ThreadStatus : TThreadStatus;
constructor Create (Suspended : boolean;PriorityIdx: integer);
procedure MyThreadExit(sender : TObject);
procedure Execute;
override;
procedure Suspend;
procedure terminate;
procedure Resume;
end;
(**************************************************************************)
implementation
uses Global, Main;
// TMyThread 創建
constructor TMyThread.Create(Suspended : boolean;PriorityIdx: integer);
begin
inherited create(Suspended);
if Suspended then
ThreadStatus:=tsSuspended
else
ThreadStatus:=tsWaiting;
//ThreadStatus:=tsRunning;
MyMutexHandle:=CreateMutex(nil,false,nil);
FPriorityIdx:=PriorityIdx;
Priority:=TThreadPriority(FPriorityIdx);
//線程級別
end;
procedure TMyThread.Execute;
begin
FreeOnTerminate:=true;
OnTerminate:=MyThreadExit;
if WaitForSingleObject(MyMutexHandle,INFINITE)=WAIT_OBJECT_0 then
begin
//Synchronize(ProcessBroadorMultiDataPacket);
Synchronize(CopyDataFromServerPc);
ThreadStatus:=tsSuspended;
end;
ReleaseMutex(MyMutexHandle);
end;
procedure TMyThread.MyThreadExit;
begin
//退出時的動作
closeHandle(MyMutexHandle);
end;
procedure TMyThread.Resume;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
procedure TMyThread.Suspend;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
procedure TMyThread.terminate;
begin
inherited;
ThreadStatus:=tsTerminated;
end;
procedure TMyThread.CopyDataFromServerPc;
var i,j: integer;
tmpstr,str: string;
buff: array[0..6000] of char;
begin
try
//....
//...
//...
Finally
//
end;
end;
end.