I have an ObservableCollection called AllVideos. I am using INotifyPropertyChanged on the video model as well as the AllVideos collection. For each file in a File Picker, I am running a method called ProcessMetaData() in a Parallel.ForEach. ProcessMetaData() adds new video file models to the AllVideos collection. The problem I am having is that the AllVideos collection is not being updated by the method. It works fine if the method is not executed in the Parallel.Foreach.
Below is the method that calls the Parallel.Foreach:
public void GetVideoFiles()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Multiselect = true;
if(openFileDialog.ShowDialog() == true)
{
List<string> files = new List<string>();
foreach(string file in openFileDialog.FileNames)
{
files.Add(file);
}
Parallel.ForEach(files, f => ProcessMetaData(f));
bool extractTrigger = true;
while (extractTrigger == true)
{
if (AllVideos.Count == files.Count)
{
Debug.WriteLine("VALUES ARE EQUAL !!!!!!!!!!");
Parallel.ForEach(AllVideos, v => ExtractFrames(v.VideoPath, v.FrameDestinationPath));
extractTrigger = false;
}
}
}
}
Below is the method being called by the Parallel.Foreach:
private void ProcessMetaData(string fileName)
{
var metaData = Helpers.MetaHelper.getExifDataViaTool(fileName);
Guid guid = Guid.NewGuid();
var destinationDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), @"FCS\Temp\" + guid.ToString() + @"\");
if (!System.IO.Directory.Exists(destinationDirectory))
{
System.IO.Directory.CreateDirectory(destinationDirectory);
System.IO.Directory.CreateDirectory(destinationDirectory + @"\Thumb\");
}
else
{
MessageBox.Show("Directy already exist");
}
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
{
AllVideos.Add(new Models.VideoFileModel()
{
VideoName = metaData["File Name"],
VideoPath = metaData["Directory"].Replace("/", @"\") + @"\" + metaData["File Name"],
VideoDuration = Helpers.MetaHelper.ParseExifDuration(metaData["Duration"]),
VideoCreated = Helpers.MetaHelper.ParseExifDateModified(metaData["Date/Time Original"]),
VideoThumbPath = destinationDirectory + "Thumb\\thumb.jpg",
FrameDestinationPath = destinationDirectory
//VideoFrameRate = Helpers.MetaHelper.GetFrameRate(fileName)
});
}));
NotifyPropertyChanged(nameof(AllVideos));
ProcessThumb(fileName, destinationDirectory);
}
Any ideas as to why AllVideos is not being updated? My code is stuck in the while loop because AllVideos.Count is always equal to 0 and not file.Count.
I would appreciate any advice! Been stuck on this one for about 8 hours.