【问题标题】:Form.Owner set from separate thread in .NET 3.5Form.Owner 从 .NET 3.5 中的单独线程设置
【发布时间】:2011-07-06 20:49:10
【问题描述】:

我正在编写一个测试应用程序,我需要将表单放在单独的线程上。

因此,如果我从主线程创建表单窗口并设置其 .Owner = 这一切正常。 如果我生成线程 UIThread 并从新线程设置 Owner 我会得到异常。 获取异常是可以理解的,因为您无法直接访问表单。 我的问题是我需要在主线程上捕获一条消息并执行 BeginInvoke 以将其推送到它的消息泵上吗?由于 UIForm ShowInTaskbar 设置为 false,我需要单击任务栏中的主应用程序并恢复其所有子窗口。

private void UIThread() // New Thread call
{
        UIForm form = new UIForm();

        form.ShowInTaskbar = false;
        form.Owner = this;

        Application.Run(form); // Expected Exception
}

【问题讨论】:

  • 你必须 pinvoke SetParent 才能解决这个问题。最好的办法是不要这样做。

标签: c# winforms multithreading .net-3.5


【解决方案1】:

我不确定,也许每个应用程序只能调用 Application.Run 一次。看看这个是否适合你

Application.Run(new Form1());
-----------------
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var thread = new Thread(
            () =>
                {
                    var form2 = new Form {Owner = this};
                });
        thread.Start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多