Z
Z
ZIK13372019-03-03 14:36:27
Delphi
ZIK1337, 2019-03-03 14:36:27

Delphi7 - variable required error when using CreateThread - how to fix?

Hello, the point is in the topic title.
I tried to make this example: codingrus.ru/readarticle.php?article_id=1998
But an error occurs when starting
YHcJoMt.jpg
Error Screen:

spoiler
ZlVqNhn.jpg

The code:
spoiler
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    TrackBar1: TTrackBar;
    TrackBar2: TTrackBar;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Execute1(Sender: TObject);
    procedure Execute2(Sender: TObject);
    procedure Button3Click(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Count1,Count2: integer;       {число вычислений в секунду в потоке}
  ThreadId1,ThreadId2: dword;   {идентификатор потока}
  HThread1, HThread2: THandle;  {указатель на поток}
      
implementation
{$R *.dfm}

    procedure TForm1.Button1Click;
      begin
         HThread1:=CreateThread (nil,0,@Execute1,nil,0,ThreadId1);
      end;

    procedure TForm1.Button2Click;
      begin
         HThread2:=CreateThread (nil,0,@Execute2,nil,0,ThreadId2);
      end;

    procedure TForm1.TrackBar1Change(Sender: TObject);
      begin
         SetThreadPriority(HThread1,TrackBar1.Position);
      end;

    procedure TForm1.TrackBar2Change(Sender: TObject);
      begin
        SetThreadPriority(HThread2,TrackBar2.Position);
      end;

    procedure TForm1.Timer1Timer(Sender: TObject);
      begin
        form1.Edit1.Text:=intTostr(Count1);  Count1:=0;
        form1.Edit2.Text:=intToStr(Count2);  Count2:=0;
      end;

    procedure TForm1.Execute1(Sender: TObject);
      var
        I, Total, Avg: integer;
      begin
        while True do
          begin
            Total:=0;
            for i:=1 to 10 do inc(Total, Random(Maxint));
            Avg:=Avg Div 10; inc(Count1);
          end;
      end;

    procedure TForm1.Execute2(Sender: TObject);
      var
        I, Total, Avg: integer;
      begin
        while True do
          begin
            Total:=0;
            for i:=1 to 10 do inc(Total, Random(Maxint));
            Avg:=Avg Div 10; inc(Count2);
          end;
      end;

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

end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2019-03-03
@Sumor

Procedure Execute1 must be outside the class, without parameters, and located before use in CreateThread (or forward must be used).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question