Delphi - 同步线程与框架
下午好 :-), 我有一个框架。这个框架是我通过主窗体动态创建的。
主要形式:
Interface := TInterface.Create(self);
with handlingInterface do begin
Parent := Form1;
Left := 0; Top := 35;
Width := 570; Height := 250;
end;
在框架中我有一个线程。我将此称为“框架中的线程”。为什么Thread和Frame可以同步?没有:
var
Form1: TForm1;
我在框架内调用线程,并且我想更改框架中进度条的位置。我不知道,为什么我可以在Thread的Synchronize方法中访问ProgressBar。
如果是 Form 中的 Thread 和 ProgressBar - 同步访问是 Form1.ProgressBar ...
但是我在 Frame 中有 Thread 和 ProgressBar。
Good afternoon :-),
I have one Frame. This Frame I dynamically created by Main Form.
Main form:
Interface := TInterface.Create(self);
with handlingInterface do begin
Parent := Form1;
Left := 0; Top := 35;
Width := 570; Height := 250;
end;
In Frame I have a Thread. I call this Thread from Frame. Why I can synchronize Thread with Frame? There isn't any:
var
Form1: TForm1;
I call Thread inside Frame and I want to change Position of ProgressBar in Frame. I don't know, why I can in Synchronize method of Thread access the ProgressBar.
If would be Thread and ProgressBar in Form - Synchronize access is Form1.ProgressBar ...
But I have Thread and ProgressBar in Frame.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您唯一要做的就是从线程更新进度条,那么有一个更轻量级的选项。我会考虑使用 PostMessage 代替。无论如何,您都不希望线程了解太多有关框架的细节。
创建线程时,为其提供框架的句柄,以便它知道将消息发布到哪里。让框架侦听 Windows 消息(其中包括进度位置)并更新进度栏。
这是一个非常简单的示例,它将进度条从 0 增加到 100,并在每次增量之间短暂休眠:
If the only thing you're trying to do is update the progress bar from the thread, there is a lighter weight option. I would consider using PostMessage instead. You don't want your thread to know too much about the details of the frame anyway.
When you create the thread, give it the handle of your frame so it knows where to post the message. Have the frame listen for the Windows message, which includes the progress position, and update the progress bar.
Here is a very simple example that increments the progress bar from 0 to 100 with a short sleep between each increment:
您可以为线程提供进度条的参考。
示例线程类。
像这样使用
You can give a reference to your progress bar to the thread.
Sample thread class.
Use like this