关一线程的挂起与VCL数据同步定义错误!

树上猪 2013-07-11 04:41:25
利用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.

...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
树上猪 2013-07-12
  • 打赏
  • 举报
回复
那另一个问题,即为何挂起不了,一执行挂起就提示错误
JohnYale 2013-07-12
  • 打赏
  • 举报
回复
挂起不了 是不是线程已经退出? 你把几个文件都贴出来 包括dfm pas和dpr 大家看看问题出在哪里
s11ss 2013-07-11
  • 打赏
  • 举报
回复
仔细看注释 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;

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧