AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein Delphi TThread, irgendwas mache ich falsch
Thema durchsuchen
Ansicht
Themen-Optionen

TThread, irgendwas mache ich falsch

Ein Thema von KodeZwerg · begonnen am 2. Mai 2018 · letzter Beitrag vom 5. Mai 2018
 
Fritzew

Registriert seit: 18. Nov 2015
Ort: Kehl
678 Beiträge
 
Delphi 11 Alexandria
 
#32

AW: TThread, irgendwas mache ich falsch

  Alt 4. Mai 2018, 09:56
Um Himmels willen........
Delphi-Quellcode:
//bzw jetzt die Friss oder Stirb methode
if not Winapi.Windows.TerminateProcess(MyThread.Handle, 0) then MyThread.Terminate;
Das ist jetzt aber nur noch falsch, schau mal in die Docu von TerminateProcess
https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx


Warum nicht einfach so etwas:
Form:
Delphi-Quellcode:
object Form5: TForm5
  Left = 0
  Top = 0
  Caption = 'Form5'
  ClientHeight = 168
  ClientWidth = 371
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 14
  object Label1: TLabel
    Left = 48
    Top = 32
    Width = 31
    Height = 13
    Caption = 'Label1'
  end
  object Button1: TButton
    Left = 48
    Top = 88
    Width = 249
    Height = 25
    Caption = 'Starte Thread'
    TabOrder = 0
    OnClick = Button1Click
  end
end

code:

Delphi-Quellcode:
unit Unit5;

interface

uses
   Winapi.Windows,
   Winapi.Messages,
   System.SysUtils,
   System.Variants,
   System.Classes,
   Vcl.Graphics,
   Vcl.Controls,
   Vcl.Forms,
   Vcl.Dialogs,
   Vcl.StdCtrls;

type
   TForm5 = class(TForm)
      Button1: TButton;
      Label1: TLabel;
      procedure Button1Click(Sender: TObject);
      procedure FormCreate(Sender: TObject);

   private
    { Private-Deklarationen }

      procedure StarteThread;
      procedure FinishTread(const Value: string);

   public
    { Public-Deklarationen }
   end;

var
   Form5: TForm5;

implementation

{$R *.dfm}

procedure TForm5.FormCreate(Sender: TObject);
begin
   Label1.Caption := '';
end;

procedure TForm5.Button1Click(Sender: TObject);
begin
   StarteThread;
end;

procedure TForm5.StarteThread;
Var temp : String;
begin
   Label1.Caption := 'Thread running';
   Button1.Enabled := False;
   TThread.CreateAnonymousThread(
      procedure
      begin
       try
         Sleep(5 * 1000); // Wait 5 secs
          temp := 'Ich habe fertig';
         // Oder halt Dein GetHttp
          //temp := GetHttp('wasauchimmer');


      // Und dem Mainthread mitteilen das wir etwas haben
      // Queue damit das erst passiert wenn der Mainthread wirklich Zeit hat.....
      if temp <> 'then
       TThread.Queue(nil,
            procedure
            begin
               FinishTread(temp);
            end);
       finally
           // Den Button wieder einschalten
          // Synchronize damit der Button sofort wieder enabled wird
         TThread.Synchronize(nil,
            procedure
            begin
               Button1.Enabled := true;
            end);
       end;

      end).Start;

end;

procedure TForm5.FinishTread(const Value: string);
begin
   Label1.Caption := Value;
end;

end.
Fritz Westermann
  Mit Zitat antworten Zitat
 

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:37 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz