0
Answer

eror:DependencySource on same Thread as the DependencyObject

Suraj Sararf

Suraj Sararf

9y
796
1
I got that error when i use a background worker.
error - Must create DependencySource on same Thread as the DependencyObject.

code:-

public FileView(string path)
        {
            InitializeComponent();
            loadlist(path);
            listview.ItemsSource = vh.list();

        }
Variableholder vh=new Variableholder();
BackgroundWorker bworker = new BackgroundWorker();
        public void loadlist(string path)
        {
            bworker.WorkerSupportsCancellation = true;
            bworker.WorkerReportsProgress = true;
            bworker.DoWork += bworker_Explorer;
            bworker.ProgressChanged += bworker_AddItem;
            bworker.RunWorkerCompleted += worker_RunWorkerCompleted;
            bworker.RunWorkerAsync(path);
        }

public void bworker_Explorer(object sender,DoWorkEventArgs e)
        {
            ParallelOptions po = new ParallelOptions();
            po.MaxDegreeOfParallelism = 10;
            Parallel.ForEach(e.Argument.ToString().Split(','),po, speratepath =>
            {
              ......
            });
         }
public void bworker_AddItem(object sender,ProgressChangedEventArgs e)
        {
                if (e.UserState != null)
                {
                    vh.list().Add(e.UserState as MusicTags);
                }
        }
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            .......
        }
private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
                        object o = listview.SelectedItem;
                        string filepath = (o as MusicTags).Filepath;
                        itemscontrol.ItemsSource = vh.list().Where(s => s.Filepath == filepath);
        }


  what i do?