I create Button in my c# kod and add this button to dictionary. I have method to run buttons. When i run this method in Task it make exception:
The calling thread cannot access this object because it belongs to a different thread.
private void RefreshTags()
    {
        var r = new Random();
        lock (dictTagModel)
        {
            foreach (var item in dictTagModel)
            {
                Canvas.SetLeft(item.Value, r.Next(1, 1400));
            }
        }
    }
    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        var r = new Random();
        listTagModel.Add(new TagModel { TagMac = r.Next(1, 10) });
        lock (dictTagModel)
        {
            foreach (var tag in listTagModel)
            {
                if (!dictTagModel.ContainsKey(tag.TagMac))
                {
                    var button1 = new Button
                    {
                        //Name = item.Angle.ToString(),
                        Background = Brushes.Black,
                        Height = 8,
                        Width = 8,
                        Content = 0.ToString(),
                        FontSize = 10,
                    };
                    Canvas.SetLeft(button1, 100);
                    Canvas.SetTop(button1, tagHight);
                    canvas.Children.Add(button1);
                    dictTagModel.Add(tag.TagMac, button1);
                    tagHight -= 10;
                }
            }
        }
        if(tagHight == 325)
        {
            Task.Run(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    RefreshTags();
                }
            });
        }
    }
When it isnt in Task and i only run 1 time this method it work.
 
     
    