我遇到的问题是我的背景工作人员在第一次按下调用它的按钮时完成工作但第二次按下按钮时会产生错误。错误标记在不在集合上的整数上。
错误是“这种类型的CollectionView不支持从与Dispatcher线程不同的线程更改其SourceCollection。”代码行是“t + = 1;”也许我需要在不同的地方定义背景工作者? 这是代码的一部分。
public P4LabelBatteryViewModel()
{
BatteryCheckerModel BatteryCheckerModel = new BatteryCheckerModel();
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.RunWorkerCompleted += Worker_WorkerCompleted;
}
private void checkOutRestults()
{
TotalFiles = 0;
foreach (var _scripObject in ScriptCollection)
{
if (_scripObject.ScriptNameAdd != "")
{
TotalFiles += 1;
}
}
if (ScriptCollection.Count > 0)
{
_isrunning = true;
worker.RunWorkerAsync();
}
}
void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
var t = e.ProgressPercentage;
NewBatteryFiles += 1;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
ScriptModel _newFileResult = new ScriptModel();
int t = 0;
_newFileResult = CheckOutResults.CheckOutResultBatteryFiles(BatteryLocation, SelectedMachine.Machine);
ScriptCollectionTemp.Add(_newFileResult);
t += 1;
foreach (ScriptModel _checkOutFile in ScriptCollection)
{
_newFileResult = CheckOutResults.CheckOutResultFiles(_checkOutFile, BatteryLocation, SelectedMachine.Machine);
ScriptCollectionTemp.Add(_newFileResult);
t += 1;
worker.ReportProgress(t);
}
//worker.ReportProgress(t);
}
void Worker_WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
foreach (ScriptModel _checkOutFile in ScriptCollectionTemp)
{
_checkOutFile.BackGround = new SolidColorBrush(Colors.LightGreen);
}
ScriptCollection = ScriptCollectionTemp;
}
答案 0 :(得分:1)
没有控件会像你在不同的线程上更改它们到UI线程。这就是世界运作的方式
忽略其他问题,此模式将允许您从与UI上下文不同的上下文更新UI
Application.Current.Dispatcher.Invoke(
() =>
{
// do any UI updates here
});
答案 1 :(得分:-1)
我按如下方式修改了我的代码,它现在已经第二次工作了。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/posixpath.py", line 129, in dirname
i = p.rfind('/') + 1
File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 3614, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'rfind'