jeurk
asked on
What's the problem with my thread ?
Hi,
Here is the code of a small program that calls a simple
thread to do nothing more then update a label.
The problem is that when I call the thread more then once
it is no working correctly. I mean that the program is
writing the value of the string in the upper left corner of
my screen. If I launch even more threads, then I get the
error : "canvas does not allow drawing"
I thought that I was doing things right by using a proc passed to the main thread with syncronize.
Here comes the code, let my know what you think about it
please. If you think the question deserves more points, let me know too. Thanks in advance for your help.
If you want me to send you the project in a zip file,
give me an email...
---------the thread unit
unit Unit2;
interface
uses
Classes;
type
TSnoopDir = class(TThread)
private
{ Private declarations }
procedure DoVisualFeedBack;
protected
procedure Execute; override;
procedure VisualFeedBack;
procedure test;
public
constructor Create;
end;
implementation
uses unit1, sysutils;
{ SnoopDir }
constructor TSnoopDir.Create;
begin
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TSnoopDir.DoVisualFeedBack ;
var
a,b :integer;
begin
a:=random(1000);
b:=random(1000);
Form1.label1.Caption := IntToStr(a);
Form1.label2.Caption := IntToStr(b);
end;
procedure TSnoopDir.VisualFeedBack() ;
begin
Synchronize(DoVisualFeedBa ck);
end;
procedure TSnoopDir.test;
var i:integer;
begin
for i := 0 to 1000 do
begin
DoVisualFeedBack;
end;
end;
procedure TSnoopDir.Execute;
begin
test;
end;
end.
---------------------
------the project--------
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo rm1, Form1);
Application.Run;
end.
---------------------
-----the form : only a button and two labels
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender : TObject);
begin
TSnoopDir.Create;
end;
end.
-------------
-------the dfm
object Form1: TForm1
Left = 499
Top = 87
Width = 159
Height = 117
Caption = 'Form1'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 16
Top = 56
Width = 32
Height = 13
Caption = 'Label1'
end
object Label2: TLabel
Left = 88
Top = 56
Width = 32
Height = 13
Caption = 'Label2'
end
object Button1: TButton
Left = 40
Top = 16
Width = 75
Height = 25
Caption = 'start'
TabOrder = 0
OnClick = Button1Click
end
end
----------------
Here is the code of a small program that calls a simple
thread to do nothing more then update a label.
The problem is that when I call the thread more then once
it is no working correctly. I mean that the program is
writing the value of the string in the upper left corner of
my screen. If I launch even more threads, then I get the
error : "canvas does not allow drawing"
I thought that I was doing things right by using a proc passed to the main thread with syncronize.
Here comes the code, let my know what you think about it
please. If you think the question deserves more points, let me know too. Thanks in advance for your help.
If you want me to send you the project in a zip file,
give me an email...
---------the thread unit
unit Unit2;
interface
uses
Classes;
type
TSnoopDir = class(TThread)
private
{ Private declarations }
procedure DoVisualFeedBack;
protected
procedure Execute; override;
procedure VisualFeedBack;
procedure test;
public
constructor Create;
end;
implementation
uses unit1, sysutils;
{ SnoopDir }
constructor TSnoopDir.Create;
begin
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TSnoopDir.DoVisualFeedBack
var
a,b :integer;
begin
a:=random(1000);
b:=random(1000);
Form1.label1.Caption := IntToStr(a);
Form1.label2.Caption := IntToStr(b);
end;
procedure TSnoopDir.VisualFeedBack()
begin
Synchronize(DoVisualFeedBa
end;
procedure TSnoopDir.test;
var i:integer;
begin
for i := 0 to 1000 do
begin
DoVisualFeedBack;
end;
end;
procedure TSnoopDir.Execute;
begin
test;
end;
end.
---------------------
------the project--------
program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas';
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TFo
Application.Run;
end.
---------------------
-----the form : only a button and two labels
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.Button1Click(Sender
begin
TSnoopDir.Create;
end;
end.
-------------
-------the dfm
object Form1: TForm1
Left = 499
Top = 87
Width = 159
Height = 117
Caption = 'Form1'
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 16
Top = 56
Width = 32
Height = 13
Caption = 'Label1'
end
object Label2: TLabel
Left = 88
Top = 56
Width = 32
Height = 13
Caption = 'Label2'
end
object Button1: TButton
Left = 40
Top = 16
Width = 75
Height = 25
Caption = 'start'
TabOrder = 0
OnClick = Button1Click
end
end
----------------
ASKER
I think you are right !
That's always the problem when you try something you don't
completly understand.
Ok, I've tested it, you are completly right !
Thanks for your help. Post me that as an answer.
Have a nice day, I just came back from lunch and the afternoon will
be tough...
CU
That's always the problem when you try something you don't
completly understand.
Ok, I've tested it, you are completly right !
Thanks for your help. Post me that as an answer.
Have a nice day, I just came back from lunch and the afternoon will
be tough...
CU
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks again
by just a fast look
procedure TSnoopDir.test;
var i:integer;
begin
for i := 0 to 1000 do
begin
DoVisualFeedBack;
end;
end;
procedure TSnoopDir.Execute;
begin
test;
end;
here is no synchronize, the method procedure TSnoopDir.VisualFeedBack()
meikl