我估计要用到多线程,我的窗体有几个按钮,但我按了button1后,其他两个都用不了了,谁能帮我改改!!代码发到我信箱里吧:chenglong5678@163.c

  • 主题发起人 苦命的人
  • 开始时间

苦命的人

Unregistered / Unconfirmed
GUEST, unregistred user!
我估计要用到多线程,我的窗体有几个按钮,但我按了button1后,其他两个都用不了了,谁能帮我改改!!代码发到我信箱里吧:chenglong5678@163.com,求您了!!(168分)<br />unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
mystring: array [0..11] of string=('a','b','c','d','e','f','4','5','6','7','8','9');

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,l: integer;
begin
memo1.lines.clear;
for i:=0 to 11do
for j:=0 to 11do
for k:=0 to 11do
for l:=0 to 11do
memo1.lines.add(mystring+mystring[j]+mystring[k]+mystring[l]);
memo1.lines.savetofile('c:/yhhe.txt');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
showmessage('这个按钮还能用!!');
end;

end.
 
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,k,l: integer;
begin
memo1.lines.clear;
for i:=0 to 11do
for j:=0 to 11do
for k:=0 to 11do
for l:=0 to 11do

begin
memo1.lines.add(mystring+mystring[j]+mystring[k]+mystring[l]);
//加上下面这句
application.processmessage;
end;
memo1.lines.savetofile('c:/yhhe.txt');
end;

 
我试试!!
 
苦命的人:
那就用多线程吧,将Button1要做的工作放在一个线程中,其他按钮就不受影响了,不过,
注意不要冲突。
下面是最简单的多线程例子,所要演示的就是在线程运行时,你可以在Memo1中随便输入,
也就是说白了,不影响你做别的事。这不正是你要的效果吗。
//////////////////////////////////////////////////////////////////////////////
//主程序
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ThrdU;
type
TMainForm = class(TForm)
Edit1: TEdit;
Button1: TButton;
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;
implementation
{$R *.DFM}
procedure TMainForm.Button1Click(Sender: TObject);
begin
TTestThread.Create(False);
end;

end.
/////////////////////////////////////////////////////////////////////////////
//线程单元
unit ThrdU;
interface
uses
Classes;
type
TTestThread = class(TThread)
private
Answer: integer;
protected
procedure GiveAnswer;
procedure Execute;
override;
end;

implementation
uses SysUtils, Main;
{ TTestThread }
procedure TTestThread.GiveAnswer;
begin
MainForm.Edit1.Text := InttoStr(Answer);
end;

procedure TTestThread.Execute;
var
I: Integer;
begin
FreeOnTerminate := True;
for I := 1 to 20000do
begin
if Terminated then
Break;
Inc(Answer, Round(Abs(Sin(Sqrt(I)))));
Synchronize(GiveAnswer);
end;
end;

end.
/////////////////////////////////////////////////////////////////////////////
 
主程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
uses
unit2;
procedure TForm1.Button1Click(Sender: TObject);
begin
TTCalc.Create;
end;

end.

线程:
unit Unit2;
interface
uses
Classes;
type
TTCalc = class(TThread)
private
{ Private declarations }
Fi: TextFile;
protected
procedure Execute;
override;
public
constructor Create;
end;

implementation
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Calc.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Calc }
constructor TTCalc.Create;
begin
inherited Create(False);
AssignFile(Fi, 'c:/yhhe.txt');
Rewrite(Fi);
end;

procedure TTCalc.Execute;
const
MyStr = 'abcdef456789';
var
i, j, k, l: integer;
begin
FreeOnTerminate := True;
{ Place thread code here }
for i := 1 to 12do
for j := 1 to 12do
for k := 1 to 12do
for l := 1 to 12do
Writeln(Fi, MyStr + MyStr[j] + MyStr[k] + MyStr[l]);
CloseFile(Fi);
end;

end.

 
多人接受答案了。
 

Similar threads

I
回复
0
查看
668
import
I
I
回复
0
查看
583
import
I
I
回复
0
查看
766
import
I
顶部