मैं जो एक कार्यकर्ता धागा चलाता है और घटनाओं आवेदन करने के लिए वापस भेजता है एक DLL - पूरी तरह से खिड़कियों रूपों पर काम किया, WPF में स्विच करने और सब कुछ काम करना बंद कर। मैं काम करने के लिए 4 घंटों तक एक ईंट की दीवार के खिलाफ अपने सिर को तोड़ रहा हूं। लेकिन जिस समाधान के साथ मैं समाप्त हुआ, माइक्रोसॉफ्ट के यूआई थ्रेड सेफ मार्शलिंग को सक्षम करने के लिए धन्यवादकोलेक्शन सिंक्रनाइज़ेशन, इसे हल करने के लिए वास्तव में एक साफ कार्यान्वयन देता है।
यह संग्रह पर्यवेक्षण चयन को बढ़ाता है और सक्षम करता है EnableCollectionSynchronization इन ऑब्जेक्ट्स को WPF और पृष्ठभूमि कार्यकर्ताओं के बीच उपयोग करने योग्य बनाता है।
संपादित: Microsoft's docs निम्नलिखित कहते हैं, तो मैं ग्रहण करने के लिए उस वस्तु संदर्भ बंटवारे कोई फर्क नहीं पड़ता जा रहा हूँ।
संदर्भ पैरामीटर एक मनमानी वस्तु है जिसे आप संग्रह सिंक्रनाइज़ेशन सक्षम करते समय ज्ञात जानकारी के लिए उपयोग कर सकते हैं। संदर्भ शून्य हो सकता है।
ThreadSafeCollection.cs
using System.Collections.ObjectModel;
using System.Windows.Data;
namespace NSYourApplication
{
/// <summary>
/// This ObservableCollection is thread safe
/// You can update it from any thread and the changes will be safely
/// marshalled to the UI Thread WPF bindings
/// Thanks Microsoft!
/// </summary>
/// <typeparam name="T">Whatever type of collection you want!</typeparam>
public class ThreadSafeCollection<T> : ObservableCollection<T>
{
private static object __threadsafelock = new object();
public ThreadSafeCollection()
{
BindingOperations.EnableCollectionSynchronization(this, __threadsafelock);
}
}
}
उदाहरण WindowViewModel WindowViewModel.cs
namespace NSYourApplication
{
/// <summary>
/// Example View
/// BaseModelView implements "PropertyChanged" to update WPF automagically
/// </summary>
class TestViewModel : BaseModelView
{
public ThreadSafeCollection<string> StringCollection { get; set; }
/// <summary>
/// background thread implemented elsewhere...
/// but it calls this method eventually ;)
/// Depending on the complexity you might want to implement
/// [MethodImpl(MethodImplOptions.Synchronized)]
/// to Synchronize multiple threads to prevent chase-conditions,deadlocks etc
/// </summary>
public void NonUIThreadMethod()
{
// No dispatchers or invokes required here!
StringCollection.Add("Some Text from a background worker");
}
/// <summary>
/// Somewhere in the UIThread code it'll call this method
/// </summary>
public void UIThreadMethod()
{
StringCollection.Add("This text come from UI Thread");
}
/// <summary>
/// Constructor, creates a thread-safe collection
/// </summary>
public TestViewModel()
{
StringCollection = new ThreadSafeCollection<string>();
}
}
}
एक XAML खिड़की/नियंत्रण में एक लिस्टबॉक्स में प्रयोग MainWindow.xaml
<ListBox x:Name="wpfStringCollection" ItemsSource="{Binding StringCollection,Mode=OneWay}">
</ListBox>
स्रोत
2017-08-21 23:21:11
के रूप में सेट करें ताकि पृष्ठभूमि थ्रेड में आइटम संग्रह में उन्हें जोड़कर UI थ्रेड पर नियंत्रण अपडेट हो जाए? वह कैसे काम करता है? – Mark
नहीं, आप उन्हें डिस्पैचर सूत्र में, लेकिन एक कम प्राथमिकता के साथ और छोटे chucnks में, एक बहुत मदद के लिए जोड़ने के लिए, तो यूआई उत्तरदायी रहता – Bubblewrap
मैं देख रहा हूँ धन्यवाद मुझे लगता है कि एक कोशिश – Mark