首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关一线程的挂起与VCL数据同步定义异常

2013-08-01 
关一线程的挂起与VCL数据同步定义错误!利用DELPHI的线程对象定义了二个线程,但里面几乎没任何操作,竞然挂

关一线程的挂起与VCL数据同步定义错误!
利用DELPHI的线程对象定义了二个线程,但里面几乎没任何操作,竞然挂起不了,提示“拒绝访问(5)”的错误;再就是在create让线程中的控件与主进程是控件同步,结果执行程序也报错,返内存溢出的错误。相关代码如下:
implementation


{$R *.dfm}
uses  sendTHD,RecvTHD;
var thread1:Sendthread;thread2:RevThread;

procedure TForm1.Button3Click(Sender: TObject);
begin
application.Terminate;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
thread1.Resume;
thread2.Resume;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
thread1:= Sendthread.Create;
thread2:= RevThread.Create;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
try
   thread1.Suspend;  //这里在挂起不了
   thread1.Free;
except
  showmessage('THread1 is not Suspend and Free');
end;
try
   thread2.Suspend;
   thread2.Free;
except
  showmessage('THread2 is not Suspend and Free');
end;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  // deletecriticalsection(CS);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
try
   Thread1.EditThd1:=edit1;  //这里也没有正常执行,并跳过显示'EditThd1 is not'提示
except
    showmessage('EditThd1 is not');
end;
end;

end.


其中一个线程对象代码:(两个一样)
unit sendTHD;

interface

uses
  Classes {$IFDEF MSWINDOWS} , Windows {$ENDIF},Messages, SysUtils, Variants, Graphics, Controls, Forms,
   Dialogs, StdCtrls;

type
  Sendthread = class(TThread)
  EditThd1:TEdit;
  private
    procedure SetName;
  protected
    procedure Execute; override;
  public
    constructor Create;
  end;
 

implementation
uses tUnitD;

 constructor  Sendthread.create;
 begin
    inherited create(true);
    FreeONTerminate:=false;
 end;
{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);



  and UpdateCaption could look like,

    procedure Sendthread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{$IFDEF MSWINDOWS}
type
  TThreadNameInfo = record
    FType: LongWord;     // must be 0x1000
    FName: PChar;        // pointer to name (in user address space)
    FThreadID: LongWord; // thread ID (-1 indicates caller thread)
    FFlags: LongWord;    // reserved for future use, must be zero
  end;
{$ENDIF}
{ Sendthread }
procedure Sendthread.SetName;
{$IFDEF MSWINDOWS}
var
  ThreadNameInfo: TThreadNameInfo;
{$ENDIF}
begin
{$IFDEF MSWINDOWS}
  ThreadNameInfo.FType := $1000;
  ThreadNameInfo.FName := 'SendthreadDemo';
  ThreadNameInfo.FThreadID := $FFFFFFFF;
  ThreadNameInfo.FFlags := 0;

  try
    RaiseException( $406D1388, 0, sizeof(ThreadNameInfo) div sizeof(LongWord), @ThreadNameInfo );
  except
  end;
{$ENDIF}
end;

procedure Sendthread.Execute;
begin
  SetName;
 form1.Edit1.Text:='Thread Edit is V';
  { Place thread code here }
end;

end.
 

[解决办法]
仔细看注释
 Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure Sendthread.UpdateCaption;


    begin
      Form1.Caption := 'Updated in a thread';
    end;
[解决办法]
挂起不了 是不是线程已经退出?
你把几个文件都贴出来 包括dfm pas和dpr 大家看看问题出在哪里

热点排行