views:

167

answers:

2

I have an application where when a person types or selects a listbox there's a portion of the screen that dynamically updates to a new view.

The problem is since WPF runs everything in a single thread the displaying of the view can interfer with typing or navigating making the app less responsive. What i'd like to do is run the view portion in a different thread.

My first thought was to use a window running on a different thread, but more than being something of a hack there's the problem of the window losing focus and being placed behind the mainwindow when the mainwindow is clicked. I could make it topmost but I also need to place other windows in front of it.

So what's the best way to achieve this, can I place the view in a frame and run it in a different thread?

+1  A: 

You can load / generate the data in a backround thread and then update the UI using Dispatcher.BeginInvoke.

winSharp93
A: 

I would propose you use the Visibility property of this piece of the screen that you want to make appear and use a trigger to set it from Invisible or Collapsed to Visible whenever the user types or selecs. Or you can animate the Opacity property to produce a cool fading effect ;-) I will add some code to illustrate the point. EDIT: a time consuming backgroundtask, like File operations, can be accomplished using a BackgroundWorker

<Window x:Class="VisibleOnTypingSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel>
    <StackPanel Orientation="Horizontal">
        <Label Name="TypingSnooper"
               Visibility="{Binding TypingSnooperVisibility}">
            You are typing!</Label>
        <Label>
            <Label.Style>
                <Style>
                    <Setter Property="Label.Opacity" Value="0"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding HasListBoxNewSelection}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation From="0" To="1"
                                                         Duration="0:0:1"
                                                         Storyboard.TargetProperty="Opacity"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation From="1" To="0"
                                                         Duration="0:0:1"
                                                         Storyboard.TargetProperty="Opacity"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Label.Style>
            You selected!
        </Label>
    </StackPanel>
    <TextBox TextChanged="TextBox_TextChanged"></TextBox>
    <ListBox Name="SimpleListBox"
             SelectionChanged="SimpleListBox_SelectionChanged">
        <ListBoxItem>1</ListBoxItem>
        <ListBoxItem>2</ListBoxItem>
    </ListBox>
</StackPanel>

    using System.Windows;
    using System.Windows.Controls;

namespace VisibleOnTypingSpike
{
    public partial class Window1 : Window
    {
        public Visibility TypingSnooperVisibility
        {
            get { return (Visibility)GetValue(TypingSnooperVisibilityProperty); }
            set { SetValue(TypingSnooperVisibilityProperty, value); }
        }

        public static readonly DependencyProperty TypingSnooperVisibilityProperty =
            DependencyProperty.Register("TypingSnooperVisibility",
                                        typeof(Visibility),
                                        typeof(Window1),
                                        new UIPropertyMetadata(System.Windows.Visibility.Collapsed));

        public bool HasListBoxNewSelection
        {
            get { return (bool)GetValue(HasListBoxNewSelectionProperty); }
            set { SetValue(HasListBoxNewSelectionProperty, value); }
        }

        public static readonly DependencyProperty HasListBoxNewSelectionProperty =
            DependencyProperty.Register("HasListBoxNewSelection",
                                        typeof(bool),
                                        typeof(Window1),
                                        new UIPropertyMetadata(false));

        public Window1()
        {
            InitializeComponent();
            DataContext = this;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textbox = (TextBox) sender;
            if (textbox.Text.Length > 0) TypingSnooperVisibility = Visibility.Visible;
            else TypingSnooperVisibility = Visibility.Hidden;
        }

        private void SimpleListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            HasListBoxNewSelection = true;
            HasListBoxNewSelection = false;
        }
    }
}
Dabblernl
Still doesnt help with the threading issue I'm afraid, the problem is that the mere function of for instance of for example loading a Document can produce "stutter" in the interface, I really need to run it in a separate thread
MattiasK
I added the code anyway ;-) Things like loading a document you can accomplish using a BackgroundWorker. I included a link to msdn in the answer above. WPF will never allow two portions of the same UI to run on seperate threads. You must do your time consuming task on the background and then notify the UI trough an event that the new data is ready.
Dabblernl
Sure I can, I can layer windows on top of each other, each running their own thread and even being transparent. It's a bit of a hack but it works. I see now reason why a frame couldnt run in it's own thread
MattiasK