救命,多线程画图 情况是这样的一个线程串口通讯,返回的数据在一个image里画线?当数据量不大的时候还好,当数据多的时候经学会出现画到一半的时候image就没有反映了。这个时候再怎么动作。image也没有反映了。为什么 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 释放CPU,application.ProcessMessages 在我的循环接收程序中有用。application.ProcessMessages我的接收程序在一下线程.画的是主线程下的image 在线程中操作主线程的Image, 必须使用线程中的同步方法(Synchroniz), 否则会出现不可预知的结果。 为什么不用PaintBox控件画,他是线程安全的。画的时候Lock以下,画完Unlock。应该没有问题。 Lock和Unlock是TCanvas的方法, 这个在TImage的Canvas中也有的。楼主可以在线程中用到Image.Canvas的地方试一下Lock和Unlock。 有问题啊,我在动态画图在在画的过程中paintbox刷新了怎么办,还是以前画的就没了 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons;type TtestThread = class(TThread) private Fbuff: TBitmap; Fbuff2: TBitmap; Fimage: TImage; Fcount: integer; procedure updateimage; public constructor CreateThread(image: TImage); procedure Execute; override; destructor Destroy; override; end; TForm1 = class(TForm) Image1: TImage; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormActivate(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private testThread: TtestThread; public end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);var bmp: TBitmap;begin DoubleBuffered := true; bmp := TBitmap.Create; bmp.Width := ClientWidth; bmp.Height := ClientHeight; Image1.Left := 0; Image1.Top := 0; Image1.AutoSize := true; Image1.Picture.Assign(bmp); FreeAndNil(bmp); testThread := TtestThread.CreateThread(Image1);end;{ TtestThread }constructor TtestThread.CreateThread(image: TImage);begin Fcount := 0; Fimage := image; Fbuff := TBitmap.Create; Fbuff2 := TBitmap.Create; Fbuff.Width := image.Picture.Bitmap.Width; Fbuff.Height := image.Picture.Bitmap.Height; Fbuff2.Width := image.Picture.Bitmap.Width; Fbuff2.Height := image.Picture.Bitmap.Height; inherited Create(TRUE); Self.Priority := tpLower;end;destructor TtestThread.Destroy;begin FreeAndNil(Fbuff); FreeAndNil(Fbuff2); inherited;end;procedure TtestThread.Execute;begin while not Terminated do begin Sleep(1); Inc(Fcount); Synchronize(updateimage); end; Terminate;end;procedure TtestThread.updateimage;begin Fbuff2.Canvas.Draw(0, 0, Fbuff); Fbuff.Canvas.Draw(0, 20, Fbuff2); Fbuff.Canvas.TextOut(5, 3, '第' + IntToStr(Fcount) + '次'); BitBlt(Fimage.Canvas.Handle, 0, 0, Fbuff.Width, Fbuff.Height, Fbuff.Canvas.Handle, 0, 0, SRCCOPY); Fimage.Canvas.TextOut(100, 3, '第' + IntToStr(Fcount) + '次'); Fimage.Parent.Update;end;procedure TForm1.FormDestroy(Sender: TObject);begin if not testThread.Terminated then testThread.Terminate; testThread.Free;end;procedure TForm1.FormActivate(Sender: TObject);begin testThread.Resume;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin if not testThread.Terminated then testThread.Terminate;end;end.我测试了 10000 次刷新object Form1: TForm1 Left = 192 Top = 107 Width = 696 Height = 480 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnActivate = FormActivate OnClose = FormClose OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object Image1: TImage Left = 176 Top = 96 Width = 105 Height = 105 endend 你的msn或QQ是多少一起来研究 image出现这种情况可能是没有用同步函数的问题 cxGrid插入一行数据问题 求助有关delphi的图表问题 数组算法问题,请高YING赐教. 问一个简单SQL语句? 错误处理程序如何再返回到程序中 下面的代码是什么意思一个菜鸟的简单问题 屏蔽事件问题??? delphi中ACEES数据库怎么备份和恢复? 网上对弈的程式实现,该用TCP 还是UDP协议?大家谈! 朋友们,介绍几个好控件给我吧! dbgrid每一行加选择框 求DPL中文手册
application.ProcessMessages
我的接收程序在一下线程.
画的是主线程下的image
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;type
TtestThread = class(TThread)
private Fbuff: TBitmap;
Fbuff2: TBitmap; Fimage: TImage;
Fcount: integer;
procedure updateimage; public
constructor CreateThread(image: TImage);
procedure Execute; override;
destructor Destroy; override;
end;
TForm1 = class(TForm)
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
testThread: TtestThread;
public end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
bmp: TBitmap;
begin DoubleBuffered := true; bmp := TBitmap.Create;
bmp.Width := ClientWidth;
bmp.Height := ClientHeight;
Image1.Left := 0;
Image1.Top := 0;
Image1.AutoSize := true;
Image1.Picture.Assign(bmp); FreeAndNil(bmp);
testThread := TtestThread.CreateThread(Image1);
end;{ TtestThread }constructor TtestThread.CreateThread(image: TImage);
begin
Fcount := 0;
Fimage := image;
Fbuff := TBitmap.Create;
Fbuff2 := TBitmap.Create; Fbuff.Width := image.Picture.Bitmap.Width;
Fbuff.Height := image.Picture.Bitmap.Height; Fbuff2.Width := image.Picture.Bitmap.Width;
Fbuff2.Height := image.Picture.Bitmap.Height; inherited Create(TRUE);
Self.Priority := tpLower;
end;destructor TtestThread.Destroy;
begin
FreeAndNil(Fbuff);
FreeAndNil(Fbuff2);
inherited;
end;procedure TtestThread.Execute;
begin
while not Terminated do
begin
Sleep(1);
Inc(Fcount);
Synchronize(updateimage); end;
Terminate;
end;procedure TtestThread.updateimage;
begin Fbuff2.Canvas.Draw(0, 0, Fbuff);
Fbuff.Canvas.Draw(0, 20, Fbuff2);
Fbuff.Canvas.TextOut(5, 3, '第' + IntToStr(Fcount) + '次');
BitBlt(Fimage.Canvas.Handle, 0, 0, Fbuff.Width, Fbuff.Height, Fbuff.Canvas.Handle, 0, 0, SRCCOPY);
Fimage.Canvas.TextOut(100, 3, '第' + IntToStr(Fcount) + '次'); Fimage.Parent.Update;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
if not testThread.Terminated then
testThread.Terminate;
testThread.Free;
end;procedure TForm1.FormActivate(Sender: TObject);
begin
testThread.Resume;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not testThread.Terminated then
testThread.Terminate;
end;end.我测试了 10000 次刷新object Form1: TForm1
Left = 192
Top = 107
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnActivate = FormActivate
OnClose = FormClose
OnCreate = FormCreate
OnDestroy = FormDestroy
PixelsPerInch = 96
TextHeight = 13
object Image1: TImage
Left = 176
Top = 96
Width = 105
Height = 105
end
end