我有一个包含文件名的列表视图。我有另一个列表视图,其中包含可能的操作来重命名这些文件。最后,我有一个显示结果预览的标签。当我想要显示预览的每个列表中选择一个对象时。您只能选择一个文件,但只能选择一个或多个操作。我使用WPF/Xaml作为我的UI。我选择用线程执行预览。线程问题“调用线程不能访问此对象,因为不同的线程拥有它”。任何解决方案
这里是我的代码的一部分:“因为不同的线程拥有它调用线程不能访问该对象”
private Thread _thread;
public MainWindow()
{
InitializeComponent();
_thread = new Thread(DoWork);
}
public void DoWork()
{
while (true)
{
FileData fileData = listViewFiles.SelectedItem as FileData; // ERROR HERE
if (fileData != null)
{
string name = fileData.FileName;
foreach (var action in _actionCollection)
{
name = action.Rename(name);
}
previewLabel.Content = name;
}
Thread.Sleep(1000);
}
}
private void listViewFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
_thread.Start();
}
在运行时出现错误对FileData fileData = listViewFiles.SelectedItem作为FileData;线。你知道我该怎么做?
http://stackoverflow.com/search?q=The+calling+thread+cannot+access+this+object+because+a+different+thread+owns+it –