Q&A

  • 곽효승씨 보세요.
예상했던데로 스레드 동기화에 문제가 있었습니다.

수정된 소스를 올리니 스레드 처리부분을 주의깊게 보싶시오.



---



unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, jpeg;



const

wm_RefreshClients = wm_User;



type

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

procedure Capture;

procedure RefreshClients (var Msg: TMessage);

message wm_RefreshClients;

public

{ Public declarations }

end;



TClientthread = class(TThread)

private

FTheradMethod: TThreadMethod;

protected

procedure Execute; override;

end;



var

Form1: TForm1;

Sendable : boolean;

ClientThread : TClientThread;



implementation



{$R *.DFM}



procedure TForm1.RefreshClients;

begin

end;



procedure TClientThread.Execute;

begin

while not Terminated do

begin

Synchronize(FTheradMethod);

Application.ProcessMessages;

Sleep(1000);

end;

end;



procedure TForm1.FormCreate(Sender: TObject);

begin

Sendable := False;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

if Sendable then

begin

Sendable := False;

Button1.Caption := 'Capture';

end

else

begin

Sendable := True;

Button1.Caption := 'Not Capture';

ClientThread := TClientThread.Create(True);



ClientThread.FTheradMethod := Capture;

ClientThread.Priority := tpNormal;



ClientThread.FreeOnTerminate := True;



ClientThread.Resume;

end;

end;



procedure TForm1.Capture;

var

JpegImage: TJpegImage;

Bmp: TBitmap;

DC: HDC;

JpgStream: TMemoryStream;

Length: integer;

FileLocation: String;

begin

while Sendable do

begin

try

try

Bmp := TBitmap.Create;

Bmp.Width := Screen.Width;

Bmp.Height := Screen.Height;



DC := GetDC(0);



BitBlt(Bmp.Canvas.Handle, 0, 0, Bmp.Width, Bmp.Height, DC, 0, 0, SRCCOPY);



Application.ProcessMessages;



DeleteFile('Test.jpg');



JpegImage := TJpegImage.Create;

JpegImage.CompressionQuality := 45;

JpegImage.Assign(Bmp);

JpegImage.SaveToFile('Test.JPG');



JpgStream := TMemoryStream.Create;

JpegImage.SaveToStream(JpgStream);

Length := JpgStream.Size;

JpgStream.Position := 0;

except

Sendable := False;

end;

finally

Bmp.Free;

ReleaseDC(0, DC);

JpgStream.Free;

JpegImage.Free;

end;

end;

end;



end.



0  COMMENTS