Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can't modify or access UI from nonUI thread. So if you still want to use different thread first thing you need to do is to add some kind of model (for more info about binding and model try search for "wpf mvvm"), then bind you <code>listViewFiles.SelectedItem</code> to some property of this model this will allow you to access SelectedValue across threads. Second you need to separate all logic that changes UI to method or use lambda so in the end it can look like this:</p> <pre><code>public void DoWork() { while (true) { FileData fileData = Model.SelectedValue; if (fileData != null) { string name = fileData.FileName; foreach (var action in _actionCollection) { name = action.Rename(name); } this.Dispatcher.Invoke((Action)()=&gt; //use Window.Dispatcher { label3.Content = fileData.FileName; label4.Content = name; }); } Thread.Sleep(1000); } } </code></pre> <p>UPD. Some additional words about synchronizing with UI: in WPF every UI object inherits from <a href="http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherobject.aspx" rel="nofollow">DispatcherObject</a> class. Thus all access to object of this type can be made only from thread in which this object was created, if you want to access DispatcherObject(DO) from another thread you need to use <code>DO.Dispatcher.Invoke(Delegate)</code> method, this will queue your code to DO thread. So in conclusion to run code in UI thread you need to use Dipatcher of any UI element in this case we use Dispatcher of Window (assume that code in window code behind). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload