陆
陆波
Unregistered / Unconfirmed
GUEST, unregistred user!
我在DLL中建了一个窗体,再在窗体中建了个线程,这个线程为什么不能访问窗体中的控件?!我用了两种方法建线程,第一种没反应,第二各(API),提示出错“内存不能为READ” ( 积分: 100 )<br />我在DLL中建了一个窗体,再在窗体中建了个线程,这个线程为什么不能执行!我用了两种方法建线程,第一种没反应,第二各(API),提示出错“内存不能为READ”
我在DLL中建了一个窗体,(经测试是没问题了的!能在窗体上用各种控件,可是就是不能不能运行新建的线程!!!
)
窗体文件如下:
//================================form
unit test111;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//线程定义
type
TMyThread = class(TThread)
private
protected
procedure Execute;
override;
public
num1:integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function MyThreadFunc(Pointer):Longint;stdcall;
begin
form1.Memo1.Lines.Add('API 运行线程') ;
end;
//=============================================
procedure TMyThread.Execute;
begin
case num1 of
1: form1.Memo1.Lines.Add('运行线程') ;
end;
freeonterminate:=true;
{ Place thread code here }
end;
//============================================
procedure TForm1.Button1Click(Sender: TObject);
var
mythread:tmythread;
begin
mythread:= tmythread.Create(true);
mythread.num1:=1;
mythread.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
hThread:Thandle;//定义一个句柄
ThreadIDWord;
begin
//创建线程,同时线程函数被调用
hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID);
if hThread=0 then
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
end;
end.
我在DLL中建了一个窗体,(经测试是没问题了的!能在窗体上用各种控件,可是就是不能不能运行新建的线程!!!
)
窗体文件如下:
//================================form
unit test111;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
//线程定义
type
TMyThread = class(TThread)
private
protected
procedure Execute;
override;
public
num1:integer;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function MyThreadFunc(Pointer):Longint;stdcall;
begin
form1.Memo1.Lines.Add('API 运行线程') ;
end;
//=============================================
procedure TMyThread.Execute;
begin
case num1 of
1: form1.Memo1.Lines.Add('运行线程') ;
end;
freeonterminate:=true;
{ Place thread code here }
end;
//============================================
procedure TForm1.Button1Click(Sender: TObject);
var
mythread:tmythread;
begin
mythread:= tmythread.Create(true);
mythread.num1:=1;
mythread.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
hThread:Thandle;//定义一个句柄
ThreadIDWord;
begin
//创建线程,同时线程函数被调用
hthread:=CreateThread(nil,0,@MyThreadfunc,nil,0,ThreadID);
if hThread=0 then
messagebox(Handle,'Didn’t Create a Thread',nil,MB_OK);
end;
end.