运行一个批处理进程,并从他们每个人报告进度每个人、批处理、并从、进度

2023-09-06 18:57:53 作者:给不了嫁衣就别动内衣@

予有文件的列表进行加密。 加密是由外部控制台应用程序报告进度已经被解析进行。 这一进展将推出异步(Process.BeginOutputReadLine处理OutputDataReceived事件是我可以从这个过程中的实时进展的唯一途径)。

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).

情景: 对于每一个文件的用户选择加密,包含该文件的信息一视图模型实例被添加到一个列表框。用户添加100个文件进行加密。每个视图模型的S有一个加密命令(我用的棱镜 DelegateCommand ),当用户点击列表项目的加密按钮,它应该被触发。

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.

除此之外,用户可以选择点击加密全部,这是一种 CompositeCommand 在呼吁每个单项加密命令父容器。 每个列表中的项具有一个进度条指示加密处理此个人文件,也有在父容器一个进度条应显示总体进展

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.

我做了一个包装,异步调用的过程和触发器上的每一次进步变化的.NET事件时,VM处理该事件,并更新其进度财产进度条势必会,而这一事件也由父容器处理它重置每个进步变化的总结整体进度条。 此包装暴露了一个EncryptAsync功能启动的基本过程。 每个项目(虚拟机)中有一个包裹,当加密命令时,它调用此方法的包装,然后创建一个进程等 所以基本上,当用户点击加密所有,100进程将开始。

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.

只是为了确保,每个文件的加密可能需要长达10分钟甚至更多。的平均是一分钟或更少。

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.

现在的我的问题是

如何限制并发进程的数量(我有什么打算?或者我应该用1进行加密1)? 这是有效的方法来做到这一点?也许有一个完全不同的,更好的方法吗? 如何确保所有这些过程(驻留在驻留在虚拟机的包装)暴露在视图模型关闭(我做了最好的加密后,我可以从内部配置的资源(即加密过程本身)但我做了包皮实施的IDisposable ,我不知道实施的IDisposable 上的虚拟机将是正确的做法,是我错了。 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 implementing IDisposable on the VM will be the right way, am I wrong.

任何设计或实际指导/参考样本对这个故事将是非常美联社preciated!

Any design or practical guidance / references to samples on this story will be very appreciated!

*处置

推荐答案

1。 2。 我会强烈建议你看看如何:使用线程池(C#和Visual MSDN上基本)。 尤其是ThreadPoolExample级 - 它的解决您的问题。

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。 如果你的意思是'后继续查看/ VM关闭加密:创建TreadPool处理的静态类

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