1

My application consists of 2 main components. Each of them has specific functionality. Each of them is half-width of parent and always visible.

I use BackgroundWorker for working with data, but problem is that also UI refresh is sometimes long operation. And the main problem is: one component freezes = application freeze.


So I looking for solution how to run each component on specific Thread.

I think that best is run each component in specific Window (= UI thread for each Window). If one component freezes other can run.

I also read article about VisualHost, but with this solution you are unable to interact with the control.


But actually I can't find solution, how to nested this component's Windows inside Main Window. Any help, please?

6
  • you are still doing things in Win form way. With WPF you should have a view model which should start its own thread if its doing cpu intense work to avoid blocking UI thread. Use async/await instead of Backgroundworker if you are on .net 4.5+ Commented Aug 31, 2016 at 15:09
  • Thank you, Steve, for quick reply. I use MVVM pattern. I think that problem is in refreshing of UI – that must be executed on main UI thread and sometimes freeze all application. Commented Aug 31, 2016 at 15:17
  • how are you refreshing the UI? Commented Aug 31, 2016 at 15:18
  • By bindings from View (.xaml) to ViewModel (.cs). Commented Aug 31, 2016 at 15:26
  • I think that is similar to my problem: stackoverflow.com/questions/10029109/…. But I don't understand the solution. Commented Aug 31, 2016 at 15:29

2 Answers 2

3

In the following article MVVM : Multithreading and Dispatching in MVVM Applications you will see how the Microsoft .NET Framework handles threads and what precautions need to be taken when a background thread wants to modify an object created by the main thread (also called the UI thread). You will see how this can cause a crash, and to avoid this crash, the main thread’s Dispatcher should be used to properly handle the operation. Hope this can help you. https://msdn.microsoft.com/en-us/magazine/dn630646.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Sarel, for tip. Actually I don't have problem with "permission" to update UI components. Problem is that it is sometimes slow operation.
like Steve mentioned Async/Await is also an option.
1

Without code is hard to help. However, the main idea is to execute long-intensive operations on the non-UI thread to avoid freezing your UI.

For example(without MVVM pattern):

 private void Button_Click(object sender, RoutedEventArgs e)
    {
        Task.Run(() =>
        {
            Thread.Sleep(3000);
            txtBox.Dispatcher.Invoke(DispatcherPriority.Normal,
                  new Action(() => { txtBox.Text = "I am a TextBox"; }));
        });
    }

With MVVM pattern:

XAML:

<TextBlock Text="{Binding Number}" FontSize="188" Foreground="Red" />
ViewModel:

ViewModel:

public int Number { get; set; }

private void UpdateNumber()
{
        Task.Run(() => 
        {
            System.Timers.Timer timer = new System.Timers.Timer(250);
            timer.Elapsed += (sender, eventArgs) =>
            { 
                Number++;
                OnPropertyChanged("Number");//No exceptions, no errors
            };
            timer.Enabled = true;
        });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.