德尔福访问冲突形式的移动按钮时 [英] Delphi Access Violation when moving button on form

查看:80
本文介绍了德尔福访问冲突形式的移动按钮时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用移动while循环检查left属性的按钮在窗体上2个按钮,但我有一个访问冲突。我exeting用的CreateThread的程序()

 的code:过程AnimButton1();
VAR ImageCount:整数;
变种B1,B2:整数;
开始  尝试       而(B2<> 187)做
       开始
           B2:= frmNotification.btnBuzina2.Left;
           frmNotification.btnBuzina2.Left:= B2 - 1;       结束;       而(B1<> 256)做
       开始
           B1:= frmNotification.btnBuzina.Left;
           frmNotification.btnBuzina.Left:= B1 - 1;       结束;  除;
  结束;结束;

但是,当我使用睡眠()与至少5毫秒为单位,我没有访问冲突,是这样的:

 程序AnimButton1();
VAR ImageCount:整数;
变种B1,B2:整数;
开始  尝试       而(B2<> 187)做
       开始
           B2:= frmNotification.btnBuzina2.Left;
           frmNotification.btnBuzina2.Left:= B2 - 1;
           睡眠(5);
       结束;       而(B1<> 256)做
       开始
           B1:= frmNotification.btnBuzina.Left;
           frmNotification.btnBuzina.Left:= B1 - 1;
           睡眠(5);
       结束;  除;
  结束;结束;

有人能帮助我找出为什么不睡眠我得到的访问冲突,并用它我不?

THX提前!


解决方案

  

我执行与的CreateThread()

程序

这是你的问题。 VCL code,只能从主UI线程调用。使用 TThread.Synchronize 来调用VCL code中的主线上。

这是说,一个计时器可能是一个更合适的解决方案,你的问题不是一个线程。

I am trying to move 2 buttons on the form using a while loop checking for the buttons left property, but I have an Access Violation. I am exeting the procedure with CreateThread()

The code:

procedure AnimButton1();
var ImageCount: integer;
var b1, b2: integer;
begin

  try

       while (b2 <> 187) do
       begin
           b2 := frmNotification.btnBuzina2.Left;
           frmNotification.btnBuzina2.Left := b2 - 1;

       end;

       while (b1 <> 256) do
       begin
           b1 := frmNotification.btnBuzina.Left;
           frmNotification.btnBuzina.Left := b1 - 1;

       end;

  except;
  end;

end;

BUT, when I use a Sleep() with at least 5 miliseconds, I dont have an access violation, like this:

procedure AnimButton1();
var ImageCount: integer;
var b1, b2: integer;
begin

  try

       while (b2 <> 187) do
       begin
           b2 := frmNotification.btnBuzina2.Left;
           frmNotification.btnBuzina2.Left := b2 - 1;
           Sleep(5);
       end;

       while (b1 <> 256) do
       begin
           b1 := frmNotification.btnBuzina.Left;
           frmNotification.btnBuzina.Left := b1 - 1;
           Sleep(5);
       end;

  except;
  end;

end;

Could someone help me to find out why without the sleep I get the access violation and with it I dont?

thx in advance!

解决方案

I am executing the procedure with CreateThread().

That is your problem. VCL code must only be called from the main UI thread. Use TThread.Synchronize to invoke the VCL code on the main thread.

That said, a timer might be a more appropriate solution to you problem than a thread.

这篇关于德尔福访问冲突形式的移动按钮时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆