1

I have this code:

public Model.Seznam<Model.Zprava> Seznam
    {
        get
        {
            return this.seznam;
        }
        set
        {
            this.seznam = value;
            this.ZmenaVlastnosti("Seznam");//ChangeProperty
        }
    }

It's for this listbox

<ListBox HorizontalAlignment="Left" Height="328" Margin="10,10,0,0" VerticalAlignment="Top" Width="285" ItemsSource="{Binding Path=Seznam}" ItemTemplateSelector="{StaticResource VyberSablony}" />

The problem is, that I am using Seznam in main thread (adding messages, which I'Ve sent - Seznam.Add(..)) and I need to add received messages from other thread.

Datsheep
  • 382
  • 1
  • 4
  • 16
  • Possible duplicate http://stackoverflow.com/questions/7839296/using-the-c-sharp-dispatcher – Ayyappan Subramanian Mar 31 '15 at 19:13
  • I've tried this: `Application.Current.Dispatcher.BeginInvoke((Action)(() => Seznam.Add(new Model.Zprava(DateTime.Now.ToString(), "Server: " + Zprava, Model.Od.Server))));` And I get "object reference not set to an instance". – Datsheep Apr 01 '15 at 11:27

2 Answers2

0

Use the dispatcher:

Application.Current.Dispatcher.BeginInvoke((Action)( () => ZmenaVlastnosti("Seznam") ));

https://msdn.microsoft.com/en-us/library/cc190824(v=vs.110).aspx

0

Solution:

public static void UiInvoke(Action a)
        {
            Application.Current.Dispatcher.Invoke(a);
        }

And how to call it:

UiInvoke(() => { Seznam.Add(new Model.Zprava(DateTime.Now.ToString(), data, Model.Od.Server)); });
Datsheep
  • 382
  • 1
  • 4
  • 16