高手請近來看看! 多線程序問題(50分)

  • 主题发起人 主题发起人 hgptm
  • 开始时间 开始时间
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.
 
procedure TMyThread.Suspend;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
改为
procedure TMyThread.Suspend;
begin
ThreadStatus:=tsSuspended;
inherited;
end;

还有这个
procedure TMyThread.Resume;
begin
inherited;
ThreadStatus:=tsSuspended;
end;
ThreadStatus:=tsSuspended;
写错了吧?
 
to:兄弟
按照你的方案測試了,還是提示"控制碼錯誤"
 
改用发送消息试试
 
問題解決了,謝謝各位
 
多人接受答案了。
 
后退
顶部