Einzelnen Beitrag anzeigen

sonny2007

Registriert seit: 27. Aug 2009
39 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#9

AW: Thread läuft nicht parallel --

  Alt 9. Jan 2014, 10:42
hiho,

mmhhhhhh.........

also ich habe es jetzt so abgeändert.

in formCreate
Delphi-Quellcode:
aktScreen1 := TBitmap.Create;
  aktScreen2 := TBitmap.Create;
  suchbild1 := TBitmap.create;
  suchBild2 := TBitmap.Create;

  suchbild1.LoadFromFile('C:\Users\blabla\Pictures\i1.bmp');
  suchBild2.LoadFromFile('C:\Users\blabla\Pictures\2.bmp');
dann der Thread Aufruf vom Button

Delphi-Quellcode:
procedure TfrmMain.searchInventar(Sender: TObject);
begin
  fstarttimer := GetTickCount;
  ScreenCapture(aktScreen1);
  ScreenCapture(aktScreen2);
  ThreadsRunning := 2;

  // Threads erzeugen aber nicht ausführen
  Thread1 := TImageSearch.Create(true);
  Thread1.OnTerminate := ThreadDone1;
  Thread1.Searchbmp.Assign(suchbild1);
  Thread1.screenbmp.Assign(aktScreen1);
  Thread1.NameThreadForDebugging('Thread1');

  Thread2 := TImageSearch.Create(true);
  Thread2.OnTerminate := ThreadDone2;
  Thread2.Searchbmp.Assign(suchbild2);
  Thread2.screenbmp.Assign(aktScreen2);
  Thread2.NameThreadForDebugging('Thread2');

  // Threads starten
  Thread1.start;
  Thread2.start;
end;
Und hier der komplette thread Part

Delphi-Quellcode:
unit imgCompareThread;

interface

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

type
  TImageSearch = class(TThread)
  private
    fScreenBMP : TBitmap;
    fSearchBMP : TBitmap;
    fxyPoint : Tpoint;
    { Private-Deklarationen }
  protected
    procedure Execute; override;
  public
    //constructor Create(CreateSuspended : boolean ;var SearchBMP,ScreenBMP : TBitmap;var xyPoint : TPoint);overload;
    constructor Create(CreateSuspended: boolean); overload;
    property ScreenBMP : TBitmap read FScreenBMP write FScreenBMP;
    property SearchBMP : TBitmap read FSearchBMP write FSearchBMP;
    property xyPoint : TPoint read fxyPoint write fxyPoint;
  end;

implementation

{ TImageSearch }

procedure TImageSearch.Execute;
var
  x,y: Integer;
  tempBMP : Tbitmap;
  rectSource,rectDest : TRect;
  toBreak : boolean;
  i: Integer;
  k: Integer;
begin
  toBreak := false;
  // ******************************************************************************
  // Inventar oben links suchen
  // ******************************************************************************
  Try
    // Declaration
    tempBMP := Tbitmap.Create;
    tempBMP.Width := FSearchBMP.Width;
    tempBMP.Height := FSearchBMP.Height;
    tempBMP.PixelFormat := FSearchBMP.PixelFormat;
    for x := 0 to FScreenBMP.Width-tempBMP.Width-1 do
      begin
        if toBreak then break;
        for y := 0 to FScreenBMP.Height-tempBMP.Height-1 do
        begin
          // Ausschnittsbereich festlegen
          rectDest := rect(0,0,tempBmp.Width,tempBmp.Height);
          rectSource := rect(x,y,x+tempBmp.Width,y+tempBmp.Height);
          tempBmp.Canvas.CopyMode := cmSrcCopy;
          // Ausschnittsbereich kopieren rectSource
          tempBmp.Canvas.CopyRect(rectDest,ScreenBMP.Canvas,rectSource);
          //imgTemp.Picture.Bitmap.Assign(tempBMP);
          if Compare2Bitmaps(60,FSearchBMP,tempBMP) = ceOK then
            begin
              toBreak := true;
              fxypoint.x := x;
              fxypoint.y := y;
              with Tform.Create(nil) do
                begin
                  FormStyle := fsStayOnTop;
                  Name := 'frmSelection2';
                  Position := poDesigned;
                  left := x-3;
                  top := y-3;
                  Width := 56;
                  Height := 56;
                  borderstyle := bsNone;
                  color := clWhite;
                  for k := 0 to 5 do
                    begin
                      visible := true;
                      sleep(100);
                      visible := false;
                      sleep(100);
                    end;
                  free;
                end;
              break;
            end;
        end;
      end;
  Finally
    tempBMP.Free;
  End;
end;

constructor TImageSearch.Create(CreateSuspended: Boolean);
begin
  fScreenBMP := TBitmap.Create;
  fSearchBMP := TBitmap.Create;
  inherited Create(CreateSuspended);
end;


end.
im OnTerminate wollte ich die XY Points holen doch die geben leider immer null zurück.
Delphi-Quellcode:
Dec(ThreadsRunning);
  xyPoint2 := Thread2.xyPoint;
  showMessage('X2: ' +inttostr(Thread2.xyPoint.X)+' Y2: '+inttostr(Thread2.xyPoint.Y));
Was mache ich da falsch? Wenn ich die Bilder einzeln suche, also ohne Thread gleicher Code, findet er sie einwandfrei.

Die Version von letzter Nacht mach auch alles richtig. Also werte werden richtig zurück gegeben.

Grüße s0n
  Mit Zitat antworten Zitat