Questions about Threads
So if I am correct and understood you right I am doing it right, but why is it not working then might you be able to help me on that? And thanks for the links will check them out.
It's not working because in your code you are calling Dispatcher from the context of a different thread, see this explanation for what's going on. As a side point you should use a Task instead of a Thread as the former offers a higher level of abstraction, proper cancellation, a managed thread pool and more.
So if I am correct and understood you right I am doing it right, but why is it not working then might you be able to help me on that? And thanks for the links will check them out.
Would you mind giving me a brief story of MVVM like what it is? I know it is Model View ViewModel. But that doesn't help me alot. I did look into that like 2months ago but I didn't get anything that was my problem. D:
Certainly. I know it can look rather complicated when you meet it for the first time but trust me it's rather simple once you have persevered with it. It probably doesn't help that, because by its very nature i.e. being a design pattern, it is open to interpretation thus there's absolutely tons of different takes on its implementation floating around the internet.
Let's take a visual/tactile approach first. Have some code to play with (this is a very basic implementation of the pattern):
Create a new WPF application and paste all of this in verbatim:
MainWindow.xaml
MainWindow.xaml.cs
MainWindowViewModel.cs
When we implement MVVM we are interested specifically in separation of concerns and encapsulation. We want our View to be just that, a UI and nothing more. It shouldn't have to know anything about the VIewModel (the business logic) and vice versa the VIewModel shouldn't need to know anything about the View. Then you have the Model which is usually where you have a database interaction layer or compute model or so on and again this should be kept separated by a clean and clearly defined interface - nothing should ever cross over between any of the layers.
Communication between layers is achieved by various abstraction patterns/mechanisms such as ViewModelLocators, Data Bindings, Dependency Injection, Inversion of Control, events, interaction requests, Dependency Properties, messages and others.
The pattern naturally promotes a clean and very testable design where one should also be able to swap any of the layers out at a later stage without needing to change any dependant code.
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:viewModel="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="150" Width="800"> <Window.DataContext> <viewModel:MainWindowViewModel/> </Window.DataContext> <Grid> <ProgressBar Minimum="{Binding Minimum}" Maximum="{Binding Maximum}" Value="{Binding Current}"/> <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding Text}"/> </Grid></Window>
namespace WpfApplication1{ public partial class MainWindow { public MainWindow() { InitializeComponent(); } }}
using System.ComponentModel;using System.Threading;using System.Threading.Tasks;namespace WpfApplication1{ internal class MainWindowViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string _text; private int _current; public MainWindowViewModel() { Text = string.Empty; Task.Run( () => { var inc = 0; while (true) { if (Current <= 0) { inc = 1; } if (Current >= 100) { inc = -1; } Current += inc; Text = $"{Current}/{Maximum}"; Thread.Sleep(50); } }); } public string Text { get { return _text; } private set { _text = value; OnPropertyChanged("Text"); } } public int Minimum { get; private set; } public int Maximum { get; } = 100; public int Current { get { return _current; } set { _current = value; OnPropertyChanged("Current"); } } private void OnPropertyChanged(string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }}
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now