C# - 将控件从线程转移到另一个线程

时间:2014-04-02 19:40:51

标签: c# wpf multithreading

窗口填充内容越多,完全加载所需的内容就越多,用户只有在完成后才能使用任何控件。

所以我想象如下:

使窗口没有控件并在另一个线程上创建它们,例如BackgroundWorker。 然后,控件将逐渐添加到主网格中,因此用户可以使用第一个控件而无需等待其余控件创建。

为了尝试将其变为现实,我已经尝试过使用某些技巧。最后一个尝试使用Invoke,实际上,我不知道它是如何工作的,也不知道如何使用。

public MainWindow()
    {
        InitializeComponent();

        Thread t = new Thread(new ThreadStart(CreateControls));
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

private void CreateControls()
    {
        Button btn = new Button();
        btn.Width = 90;
        btn.Height = 30;
        btn.Background = Brushes.Black;
        btn.Foreground = Brushes.White;
        btn.Content = "Click me";

        this.Dispatcher.BeginInvoke((Action)delegate
        {
            Dispatcher.Invoke((Action)(() =>
            {
                MainGD.Children.Add(btn);
            }));
        });
    }

MainGD.Children.Add(btn);我收到: 调用线程无法访问此对象,因为其他线程拥有该对象。

在我的研究中,我找到了将按钮创建放在Invoke中的方法,如下所示:

this.Dispatcher.BeginInvoke((Action)delegate
    {
        Dispatcher.Invoke((Action)(() =>
        {
            Button btn = new Button();
            btn.Width = 90;
            btn.Height = 30;
            btn.Background = Brushes.Black;
            btn.Foreground = Brushes.White;
            btn.Content = "Click me";

            MainGD.Children.Add(btn);
        }));
    });

但我不想在主线程中创建控件 任何人都可以帮我这个吗? 这有可能吗?


重点是:是否可以在另一个线程内创建GUI控件并将其传输到GUI线程?这就是我想要做的事情。

2 个答案:

答案 0 :(得分:1)

好主意,但这恐怕不行。用户界面代码必须在GUI线程上。

BackgroundWorkers和线程用于执行工作,GUI线程用于执行用户界面。 “工作”是数字运算或数据库查找之类的东西:最终将其结果发送到GUI线程以供显示的东西。

如果你的表单需要花费很长时间来加载纯粹来自添加控件,那么可能有太多的控件 - 看看你是否可以重新设计它。

答案 1 :(得分:0)

听起来像async / await可能在这里有用。我参与了一个项目,我们的Window UI包含顶部的一些菜单项,中间的自定义控件区域,以及底部的一些操作按钮(如Ok,Cancel)。创建窗口时会创建菜单项和操作按钮。但是,中间区域中的控件是使用异步调用生成的。

我们使用ObservableCollection作为中间区域中ItemsControl的数据源。然后,在Window.ContentRendered事件中(所以在绘制窗口之后)我们等待"等待"一种构建自定义控件的方法。

这是MyWindow代码隐藏的片段:

public partial class MyWindow : Window
{
    /* Window c'tor, other methods/props ommited */

    // Load our custom controls asynchronously after the window is rendered
    private async void MyWindow_ContentRendered(object sender, EventArgs e)
    {
        // kick off the asynchronous call to create a custom controls
        await CreateCustomControls();
    }

    // Asynchronously adds MyCustomControls to the ObservableCollection 
    // (we actually used a separate static class that contained our definitions
    // and created the controls) 
    private async Task CreateCustomControls()
    {
        // the def describes what you want to build and is not detailed here
        foreach (var controlDef in MyCustomControlDefinitions)
        {
            // create a control based on the definition
            var newControl = await CreateMyCustomControl(controlDef);

            // Don't add the control until it is completely built
            MyCustomControls.Add(newControl);
        }
    }

    //The (bindable) collection of custom controls
    public ObservableCollection<MyCustomControl> MyCustomControls
    {
        get { return (ObservableCollection<MyCustomControl>)GetValue(MyCustomControlsProperty); }
        set { SetValue(MyCustomControlsProperty, value); }
    }

    public static readonly DependencyProperty MyCustomControlsProperty =
    DependencyProperty.Register("MyCustomControls", typeof(ObservableCollection<MyCustomControl>),
        typeof(MyWindow), new PropertyMetadata(new ObservableCollection<MyCustomControl>()));

}

这是MyWindow XAML的片段:

<Window>
    <Grid>
        <!-- Scrollable Panel for MyCustomControls -->
        <ScrollViewer>
            <ItemsControl ItemsSource="{Binding MyCustomControls}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>
    </Grid>
</Window>

重要的是要注意它只提供了一种感知的&#34;性能提升。我们所做的就是在创建其余控件时让窗口有用。因此,用户可以立即与顶部和底部控件交互(当显示窗口时),并且一旦将自定义控件添加到ObservableCollection,用户也可以与它们进行交互。

希望这有帮助。