Delphi Thread

Başlatan z, 02 Kasım 2019, 05:09:03

z

Bir tane image nesnemiz olsun.

Image nesnesine sin fonksiyonunun grafigi pixel pixel cizelim. Oyleki cizim hizi dusuk oldugu icin ekranda sinus adim adim cizilsin. Image nesnesinin en sagina pixel koyunca imagei silip tekrar sinus cizimi baslasin.

Bir tane de scrollbar olsun.

Bu scrollbari surukledigimizde sinusun genligi degissin.

Fakat scrollbari mousla tikladigimda sinus cizimi duraksamasin.

Bunun icin kod paylasabilirmisiniz?
Bana e^st de diyebilirsiniz.   www.cncdesigner.com

M.Salim GÜLLÜCE

#1
Paskala pek aşina değilim.
Ancak daha yeni sıfırdan başladım.
RAD 10.3.3 ü daha dün kurdum.
Demo örneklere bakarken grafik değilde, animasyon türü scrollbar kontrollü programa rastladım.
Nasıl yapıldığını anlamaya çalıştıysamda pek anlamadım.

Önce Form Kodunu gönderip arkasından yazılım kodunu yollasam sen bişeyler elde edebilirsin diye düşünüyorum.

object ActivityIndicatorForm: TActivityIndicatorForm
  Left = 0
  Top = 0
  Caption = 'TActivityIndicator'
  ClientHeight = 328
  ClientWidth = 452
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -12
  Font.Name = 'Segoe UI'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnCreate = FormCreate
  DesignSize = (
    452
    328)
  PixelsPerInch = 96
  TextHeight = 15
  object lblFrameDelay: TLabel
    Left = 245
    Top = 55
    Width = 88
    Height = 15
    Anchors = [akLeft, akTop, akBottom]
    Caption = 'Frame Delay (50)'
    OnClick = lblFrameDelayClick
  end
  object lblVclStyle: TLabel
    Left = 20
    Top = 124
    Width = 49
    Height = 15
    Alignment = taRightJustify
    Caption = 'VCL Style'
  end
  object lblFormColor: TLabel
    Left = 245
    Top = 126
    Width = 176
    Height = 15
    Caption = 'Form Color (Windows Style Only)'
  end
  object chkAnimate: TCheckBox
    Left = 245
    Top = 20
    Width = 96
    Height = 17
    Caption = 'Animate'
    TabOrder = 0
    OnClick = chkAnimateClick
  end
  object trkFrameDelay: TTrackBar
    Left = 238
    Top = 74
    Width = 203
    Height = 28
    Max = 15
    Min = 3
    Position = 5
    TabOrder = 1
    OnChange = trkFrameDelayChange
  end
  object grpIndicatorType: TRadioGroup
    Left = 20
    Top = 193
    Width = 145
    Height = 117
    Caption = 'Indicator Type'
    ItemIndex = 0
    Items.Strings = (
      'aitMomentumDots'
      'aitRotatingSector'
      'aitSectorRing')
    TabOrder = 4
    OnClick = grpIndicatorTypeClick
  end
  object grpIndicatorSize: TRadioGroup
    Left = 184
    Top = 193
    Width = 125
    Height = 117
    Caption = 'Indicator Size'
    ItemIndex = 1
    Items.Strings = (
      'aisSmall'
      'aisMedium'
      'aisLarge'
      'aisXLarge')
    TabOrder = 5
    OnClick = grpIndicatorSizeClick
  end
  object grpIndicatorColor: TRadioGroup
    Left = 328
    Top = 193
    Width = 105
    Height = 117
    Caption = 'Indicator Color'
    ItemIndex = 0
    Items.Strings = (
      'aicBlack'
      'aicWhite')
    TabOrder = 6
    OnClick = grpIndicatorColorClick
  end
  object cbxVclStyles: TComboBox
    Left = 20
    Top = 145
    Width = 197
    Height = 23
    Style = csDropDownList
    TabOrder = 2
    OnChange = cbxVclStylesChange
  end
  object AI: TActivityIndicator
    Left = 20
    Top = 20
    Anchors = [akLeft, akTop, akRight, akBottom]
  end
  object cbxFormColor: TColorBox
    Left = 245
    Top = 147
    Width = 188
    Height = 22
    DefaultColorColor = clBtnFace
    NoneColorColor = clBtnFace
    Selected = clBtnFace
    TabOrder = 3
    OnChange = cbxFormColorChange
  end
end



//---------------------------------------------------------------------------

// This software is Copyright (c) 2015 Embarcadero Technologies, Inc.
// You may only use this software if you are an authorized licensee
// of an Embarcadero developer tools product.
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.

//---------------------------------------------------------------------------
//345678901234567890123456789012345678901234567890123456789012345678901234567890
unit uActivityIndicator;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  System.ImageList,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.WinXCtrls,
  Vcl.StdCtrls,
  Vcl.ImgList,
  Vcl.ComCtrls,
  Vcl.ExtCtrls;

type
  TActivityIndicatorForm = class(TForm)
    chkAnimate: TCheckBox;
    trkFrameDelay: TTrackBar;
    lblFrameDelay: TLabel;
    grpIndicatorType: TRadioGroup;
    grpIndicatorSize: TRadioGroup;
    grpIndicatorColor: TRadioGroup;
    cbxVclStyles: TComboBox;
    lblVclStyle: TLabel;
    AI: TActivityIndicator;
    cbxFormColor: TColorBox;
    lblFormColor: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure chkAnimateClick(Sender: TObject);
    procedure trkFrameDelayChange(Sender: TObject);
    procedure grpIndicatorTypeClick(Sender: TObject);
    procedure grpIndicatorSizeClick(Sender: TObject);
    procedure grpIndicatorColorClick(Sender: TObject);
    procedure cbxVclStylesChange(Sender: TObject);
    procedure cbxFormColorChange(Sender: TObject);
    procedure lblFrameDelayClick(Sender: TObject);
  private
  public
  end;

var
  ActivityIndicatorForm: TActivityIndicatorForm;

implementation

{$R *.dfm}

uses
  Vcl.Themes;

procedure TActivityIndicatorForm.FormCreate(Sender: TObject);                          //Form-Create in
var
  StyleName: string;
  Name: string;
begin
  for StyleName in TStyleManager.StyleNames do
  cbxVclStyles.Items.Add(StyleName);

  cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name);


end;

procedure TActivityIndicatorForm.cbxFormColorChange(Sender: TObject);                  //Form-Color Selector
begin
  Color := cbxFormColor.Selected;
end;

procedure TActivityIndicatorForm.cbxVclStylesChange(Sender: TObject);                  //Form-Color Style Selector
begin
  TStyleManager.SetStyle(cbxVclStyles.Text);
  lblFormColor.Enabled := StyleServices.IsSystemStyle;
  cbxFormColor.Enabled := StyleServices.IsSystemStyle;
end;

procedure TActivityIndicatorForm.grpIndicatorColorClick(Sender: TObject);              //Indicator-Color Selector
begin
  AI.IndicatorColor := TActivityIndicatorColor(grpIndicatorColor.ItemIndex);
end;

procedure TActivityIndicatorForm.grpIndicatorSizeClick(Sender: TObject);               //Indicator-Size Selector
begin
  AI.IndicatorSize := TActivityIndicatorSize(grpIndicatorSize.ItemIndex);
end;

procedure TActivityIndicatorForm.grpIndicatorTypeClick(Sender: TObject);               //Indicator-Type Selector
begin
  AI.IndicatorType := TActivityIndicatorType(grpIndicatorType.ItemIndex);
end;

procedure TActivityIndicatorForm.lblFrameDelayClick(Sender: TObject);
begin
ShowMessage ('Çıkmaaa...'+inttostr(25));
Exit
end;

procedure TActivityIndicatorForm.trkFrameDelayChange(Sender: TObject);                 //Frame-Delay Selector
begin
  AI.FrameDelay := trkFrameDelay.Position * 10;
  lblFrameDelay.Caption := 'Frame Delay (' + IntToStr(AI.FrameDelay) + ')';
end;

procedure TActivityIndicatorForm.chkAnimateClick(Sender: TObject);                     //Animate-Enable Selector
begin
  AI.Animate := chkAnimate.Checked;
end;


end.

Umarım işine yarar. ;)

Yuunus

#2
uzun olmus

Yuunus

#3
unit Unit1;

interface

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

type
  Benim_thread=class(TThread)
    protected
      procedure execute; override;
      procedure AddLines;
    end;

  TForm1 = class(TForm)
    Image1: TImage;
    Button1: TButton;
    Button3: TButton;
    Button6: TButton;
    TrackBar1: TTrackBar;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
    thread_turet:Benim_thread;
  end;

var
  Form1: TForm1;

implementation

procedure Benim_thread.AddLines;
begin
  Sleep(10);
  Form1.Image1.Canvas.Pen.Width:=Form1.TrackBar1.Position;
  Form1.Image1.Canvas.LineTo(Random(1000),Random(1000));
  Form1.Image1.Refresh;
end;

procedure Benim_thread.execute;
begin
  repeat
    Synchronize(AddLines);
  until Terminated;
end;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
thread_turet:=Benim_thread.Create(False);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Image1.Canvas.Pen.Width:=2;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
Image1.Picture:=nil;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  thread_turet.Terminate;
end;

end.

z

#4
Ornekler icin tesekkurler. RAD yuklu olmadigi icin Delphi 7 ile @digiman'in ornegini calistirdim.

Fakat form uzerine bir tane scrollbar ekledigimde thread calisirken scrollbar uzerinde fareyi tikli vaziyette tutarsam thread icinde grafik cizen fonksiyon  duruyor. Taaki scrollbari serbest birakana kadar.

Halbuki derdim bu tip durmalarin onune gecmek.

Not: Scrollbarla ilgili hic kod yazmadim. Oylesine form uzerinde duran bir scrollbar o.

Nerde hata yapiyorum?
Bana e^st de diyebilirsiniz.   www.cncdesigner.com

Yuunus

@z hocam progressbar ile isimiz yok, Track bar ile deneyebiliriz; kodu duzenledim tekrar dener misiniz? Lakin yanlis hatirlamiyorsam Synchronize metodu verilerin guvenligini garanti etmez gibi bir ibare kalmis zihnimde, aklinizda bulunsun.

power20

#6
@digiman, sleepi execute dışında kullanırsanız donma yapar. execute dışında çalışan Thread'e ait prosedür ve fonksiyonlar hep ana thread(gui thread)de çalışır.
Sleep(1000) ile abartırsak donma durumunu rahatça görürüz.

 Böyle olması uygun. Donma yok

procedure Benim_thread.AddLines;
begin

  Form1.Image1.Canvas.Pen.Width:=Form1.TrackBar1.Position;
  Form1.Image1.Canvas.LineTo(Random(1000),Random(1000));
  Form1.Image1.Refresh;
end;

procedure Benim_thread.execute;
begin
  repeat
  Sleep(10);
    Synchronize(AddLines);
  until Terminated;
end;

Yuunus

#7
sleep ile bir isimiz yok zaten kullanmak ta mantiksiz; sadece etkiyi gorebilmek icin biraz yavaslatmak gerekiyor du. benim anlamadigim madem delphi konusunda bu kadar bilgilisiniz neden daha once bizi aydinlatmadaniz?

power20

#8
 Execute içindeki sürekli döngü arasına sleep koymaz iseniz uygulama cpu kullanımı tavan yapacaktır. Çok çekirdekli makinede %50 gibi görünür. Döngü içinde sleep yoksa başka threadlere çalışacak zaman kalmamış olur. Dolayısıyla ana form thread de  çalışamaz.

Tek çekirdekli makinede ise komple donmuş olur.

Belki  synchronize  nedeniyle sleep e gerek yoktur