I have a number of progress bars each bound to their own int property which updates their progress. Behind the scenes, I have a method that is being run multiple times which does my processing (looping through data) which I use to use to update the property which updates the progress bar.
The problem I'm having is you cannot pass a property to the method. I could create multiple copies of the method, each referring to the specific property per progress bar but this would require a lot of duplicate code. How would one recommend I go about passing the property to the method?
Essentially this is what I am trying to do but obviously passing the property by ref won't work.
Method(fileList, errorList, _ViewModel.ProgressBarProp1);
Method(fileList, errorList, _ViewModel.ProgressBarProp2);
...
private static void Method(IEnumerable<string> fileList, List<Tuple<string,
string>> errorList, ref int PropertyInt)
{
foreach (var file in fileList)
{
if (!File.Exists(file))
{
errorList.Add(new Tuple<string, string>(file,
" does not exist in the folder"));
}
PropertyInt++;
}
}
I have seen this question but this deals with Strings and I haven't been able to implement any of the solutions for an Integer scenario.
UPDATE
Implementing Mike's solution below is allowing access to the property from within the method but I'm having some odd behaviour with the way I am running this concurrently.
I'm implementing the code like so, two methods run simultaneously but incrementing the same property value (i.e. both are associated to the same progressbar).
var taskList = new List<Task>
{
Task.Run(() =>
{
Method1(fileList, errorList, p => _ViewModel.ProgressBarProp1 = p);
}),
Task.Run(() =>
{
Method2(fileList, errorList, p => _ViewModel.ProgressBarProp1 = p);
})
};
await Task.WhenAll(taskList.ToArray());
However, only one appears to be updating. What would be the reason and is there a work around?