eror:与DependencyObject在同一个Thread上的DependencySource [英] eror:DependencySource on same Thread as the DependencyObject

查看:90
本文介绍了eror:与DependencyObject在同一个Thread上的DependencySource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用后台工作程序时出现错误。

错误 - 必须在与DependencyObject相同的线程上创建DependencySource。



代码: -



public FileView(字符串路径)

{

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()。添加(e.UserState as MusicTags);

}

}

void worker_RunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e)

{

.......

}

private void listview_SelectionChanged(object sender,SelectionChangedEventArgs e)

{

object o = listview.SelectedItem;

string filepath =(o作为标签).Filepath;

itemscontrol.ItemsSource = vh.list()。其中​​(s => s.Filepath == filepath);

}





我做什么?

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 Tags).Filepath;
itemscontrol.ItemsSource = vh.list().Where(s => s.Filepath == filepath);
}


what i do?

推荐答案

粗略地讲,与op相关的一切某些UI系统的操作应该与此UI位于同一个线程上,即调用 Application.Run 的UI。为了实现这一点,当你想在一些非UI线程中对UI做一些事情时,应该使用UI调用机制。



你不能调用任何与UI相关的东西来自非UI线程。相反,您需要使用 Invoke System.Windows.Threading的方法。 Dispatcher (对于Forms或WPF)或 System.Windows.Forms.Control (仅限表单)。



您将在我过去的答案中找到有关其工作原理和代码示例的详细说明:

Control.Invoke()与Control.BeginInvoke() [ ^ ],

使用Treeview扫描仪和MD5的问题 [ ^ ]。



另请参阅有关线程的更多参考资料:

如何让keydown事件在不同的操作上运行vb.net中的线程 [ ^ ],

在启用禁用+多线程后控制事件未触发 [ ^ ]。



-SA
Roughly speaking, everything related to operations with some UI system should be on the same thread as this UI, the one where Application.Run is called. To achieve that, the UI invocation mechanism should be used when you want to do something with the UI in some non-UI thread.

You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于eror:与DependencyObject在同一个Thread上的DependencySource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆