I have a list of files to encrypt.
The encryption is made by an external console application process reporting progress that has to be parsed.
This progress has to be launched async (Process.BeginOutputReadLine handling the OutputDataReceived event is the only way I can get realtime progress from this process).
Scenario:
For each file the user selects to encrypt, a ViewModel
instance containing this file info is added to a ListBox
. The user added 100 files for encryption. Each of the ViewModel
s has an Encrypt
command (I use Prism DelegateCommand
) which should be triggered when user clicks the Encrypt button on the list item.
Besides that, the user may choose to click "Encrypt All", which is a CompositeCommand
in the parent container that calls the Encrypt command on each individual item.
Each of the items in the list has a progress bar indicating encryption process for this individual file, and there is also a progress bar in the parent container that should show the overall progress.
I made a wrapper that asynchronously calls that process and triggers a .NET event on each progress change, the VM handles that event and updates its progress property the progress bar is bound to, and this event is also handled by the parent container which resets the summary overall progress bar on each progress change.
This wrapper exposes an "EncryptAsync" function that starts the underlying process.
Each of the items (the VMs) has a wrapper in it, and when the Encrypt command is called, it calls this method on the wrapper which then creates a process etc.
So basically when user clicks Encrypt All, 100 processes will start.
Just to make sure, the encryption for each file can take up to 10 minutes and even more. The average is a minute or less.
Now my issue is:
- How do I limit the amount of simultaneous processes (do I have to? or perhaps should I encrypt them 1 by 1)?
- Is this the efficient way to do this? Maybe there is a whole different and better approach?
- How do I make sure all these processes (which reside in the wrappers that reside in the VMs) are exposed when the ViewModel shuts down (I did the best I could internally disposing the resources (i.e. the encryption process itself) after encryption but I made the wrapper implement
IDisposable
, I am not sure implementingIDisposable
on the VM will be the right way, am I wrong.
Any design or practical guidance / references to samples on this story will be very appreciated!
*Dispose