Delphi 相当于 C# 的表单调用操作是什么(如何在 Delphi 中调用)?

发布于 2024-07-25 04:23:08 字数 793 浏览 2 评论 0原文

在我的 C# 项目中,我使用以下代码在其自己的类之外访问我的表单。

public static FormMain singletonInstance { get; private set; }

    public static void PFA(Action<FormMain> action) //PFA = PerForm Action
    {
        var form = FormMain.singletonInstance;
        if (form != null)
        {

            form.PerformAction(action);
        }
    }

    public void PerformAction(Action<FormMain> action)
    {
        if (InvokeRequired)
            Invoke(action, this);
        else
            action(this);
    }

而“FormMain”中的构造函数是:

    public FormMain()
    {
        InitializeComponent();
        singletonInstance = this;
    }

我不知道如何在Delphi中做到这一点。

问题(Delphi):当我在其类之外调用时,如何更改表单上的某些内容(例如 Edit1.Text)?

提前致谢!

In my C# project,I used the following code to access my form outside its own class.

public static FormMain singletonInstance { get; private set; }

    public static void PFA(Action<FormMain> action) //PFA = PerForm Action
    {
        var form = FormMain.singletonInstance;
        if (form != null)
        {

            form.PerformAction(action);
        }
    }

    public void PerformAction(Action<FormMain> action)
    {
        if (InvokeRequired)
            Invoke(action, this);
        else
            action(this);
    }

And the constructor in "FormMain" is:

    public FormMain()
    {
        InitializeComponent();
        singletonInstance = this;
    }

I don't know how to do it in Delphi.

Question(Delphi): How do I change something on form(for example Edit1.Text) when Im outside its class,Invoke?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪花飘飘的天空 2024-08-01 04:23:08

Invoke() 操作的需要与其类内部或外部的调用代码无关,有必要确保仅在线程上下文中调用对控件进行操作的方法控件具有线程关联性,这与 C# 或 .NET 无关,它是 Windows 工作方式所固有的。 控件/窗口的消息只能在创建它的线程中处理。 这对于 Delphi 也是如此,并且整个 VCL 也不是线程安全的。

Delphi VCL 中与 Invoke() 最接近的是 Synchronize() 方法。 它在辅助线程中使用,以安排在主 VCL 线程的上下文中执行的代码,主 VCL 线程是需要在其中创建所有 VCL 控件的用户界面线程,因此在其中处理这些控件的所有 Windows 消息,并且唯一真正可以安全地调用任何控制方法的方法。

Delphi 程序不会自行创建任何辅助线程,您必须自己创建。 但除非您这样做,否则一切都发生在主线程上下文中,并且无需调用 Synchronize()。 您只需调用您需要调用的方法即可。

继续您的示例:您为编辑控件设置新文本,如下所示:

Form1.Edit1.Text := 'foo bar baz';

这假设包含 TForm1 类的单元包含变量 Form1,通常情况下都是如此。 这类似于 C# 代码中的单例。 它之所以有效,是因为放置到表单上的所有控件都将具有公共可见性。

离题评论

请注意,虽然这是相当常见的代码,但它违反了 得墨忒耳定律。 更改表单中的控件将需要随后更改以这种方式访问​​它们的所有代码。 但这完全是另一个讨论。

The need to Invoke() an action has nothing to do with the calling code being inside or outside its class, it's necessary to make sure that methods operating on controls are called only in the context of the thread the control was created in. Controls have a thread affinity, which is nothing specific to C# or .NET, it is inherent in the way Windows works. Messages for a control / window can be handled in the thread it was created in only. This holds true for Delphi as well, and the whole VCL is not thread-safe either.

The closest thing to Invoke() the Delphi VCL has is the Synchronize() method. It is used within secondary threads to schedule code to be executed in the context of the main VCL thread, the user interface thread in which all VCL controls need to be created, in which consequently all Windows messages for those controls are handled, and which is the only one really safe for calling any of the control methods.

A Delphi program does not create any secondary threads on its own, you will have to do this yourself. But unless you do, everything happens in the main thread context and there is no need to call Synchronize(). You can simply call the method you need to call.

To stay with your example: You set new text for an edit control like so:

Form1.Edit1.Text := 'foo bar baz';

This assumes that the unit containing the TForm1 class contains a variable Form1, which it usually does. This is similar to the singleton in your C# code. It works because all controls dropped onto the form will have public visibility.

Off-topic-remark:

Note that while this is fairly common code it violates the Law of Demeter. Changing the controls in the form will make it necessary to subsequently change all code that accesses them in this way. But this is another discussion entirely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文