Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What's the problem with my thread ?

Posted on 1999-07-27
4
Medium Priority
?
145 Views
Last Modified: 2011-09-20
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(DoVisualFeedBack);
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(TForm1, 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
----------------
0
Comment
Question by:jeurk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1391388
hi jeurk,

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(); is never called.

meikl
0
 
LVL 4

Author Comment

by:jeurk
ID: 1391389
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
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 1391390
hi again jeurk,

glad that i helped you
good luck again

meikl ;-)
0
 
LVL 4

Author Comment

by:jeurk
ID: 1391391
Thanks again
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question