1

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 ViewModels 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:

  1. How do I limit the amount of simultaneous processes (do I have to? or perhaps should I encrypt them 1 by 1)?
  2. Is this the efficient way to do this? Maybe there is a whole different and better approach?
  3. 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 implementing IDisposable 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

2 Answers 2

1

1. and 2.
I would highly recommend that you look at How to: Use a Thread Pool (C# and Visual Basic) on MSDN.
Especially the ThreadPoolExample-class - it has the solution for your problem

3.
If you mean 'continue encrypting after the View/VM is closed': Create the TreadPool-handling in a static class.

2
  • response on 3 - no - it's never ever supposed to convert after the VM is closed. I just want to make sure that when the VM is down (or even further more - the item is removed from the list), it should dispose the underlying wrapper->process), basically I need to know have more control on the VM, I think it's a separate question which is "How to dispose a VM with Prism", I'm sure I'll find some info on Google. Thanks. Nov 2, 2011 at 2:33
  • Please take a look at this post. Are you sure I should use the ThreadPool? I would really love to, the question is if it's a good idea. Nov 3, 2011 at 1:37
1

We have a similar issue with generating reports in a service where dozens of long running reports could be requested simultaneously. The way that we handle resource consumption is that we spin each report off into its own thread, but limit the maximum number of executing threads to a set number.

Each thread that is spawned is recorded in a dictionary variable using a key assigned to the thread at the time it is spawned. The main control process performs continuous loops (sleeping for perhaps 100ms per loop) and checks each of the running threads for completion on each pass.

As soon as a thread is identified as having completed, it is removed from the dictionary and the next one in the queue is spawned into a new thread.

Because the list of executing threads is always available, we can handle early termination requests and other unusual activities gracefully. If we needed to provide process to the users, it would be very easy to embed in the activity flow.

As a point of reference, the reporting threads that are spawned are actually classes that contain the Thread object that they are executing on. This allows us to control the Thread or provide feedback to the parent process without jumping through a lot of hoops (just make sure that you synclock any resources that are accessed from both outside and inside the thread).

1
  • I decided to use the ThreadPool. It literally does the job for me. View Michael's answer. Nov 3, 2011 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.