添加进度条到cryptostream [英] Add a progress bar to cryptostream

查看:81
本文介绍了添加进度条到cryptostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个cryptostream,并希望在此流中添加一个进度条,让用户知道它们的加密/解密程度。



进度bar被称为progressbar1



cryptoStream代码:



I have a cryptostream and would like to add a progress bar to this stream to let the user know how far through an encryption/decryption they are.

progress bar is called progressbar1

The cryptoStream code:

using (FileStream inputFileStream = File.Open(inputFile, FileMode.Open))
               using (FileStream outputFileStream = File.Open(encryptedFile, FileMode.Create))
               {
                   using (DESCryptoServiceProvider tds = new DESCryptoServiceProvider())
                   {
                       tds.Key = keyBytes;
                       tds.IV = ivBytes;

                       ICryptoTransform cryptoTransform = tds.CreateEncryptor();

                       using (CryptoStream cryptoStream = new CryptoStream(outputFileStream, cryptoTransform, CryptoStreamMode.Write))
                       {
                           byte[] buffer = new byte[inputFileStream.Length];
                           inputFileStream.Read(buffer, 0, buffer.Length);
                           cryptoStream.Write(buffer, 0, buffer.Length);
                           cryptoStream.Close();

推荐答案

您可以使用此进度条: https://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar%28v = vs.110%29.aspx [ ^ ]。



但是你没有显示进度,可能还有你不需要显示它 - 你一次读取和写入所有数据。



为了显示进度,你应该打破你的流。在循环中,您需要总结从输入读取的字节数,并将其用作,如果最大是输入流的总大小。或者您可以对输出流进行类似的计算,但是在编写之前需要对其完整大小进行正确的评估。



如果您在单独的计算中进行计算线程(在大多数情况下推荐),您需要直接通知UI进度条更新,但在UI线程中,使用 Dispatcher.Invoke Dispatcher.BeginInvoke 。请查看我过去的答案:

Control.Invoke( )与Control.BeginInvoke() [ ^ ],

Treeview扫描仪和MD5的问题 [ ^ ]。



-SA
You can use this progress bar: https://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar%28v=vs.110%29.aspx[^].

But you don't have progress to show, and probably you don't need to show it — you read and write all data at once.

For showing progress, you should break your stream in chunks. In your loop, you need to summarize the number of bytes read from input and use it as Value if the Maximum is the total size of the input stream. Or you can do similar calculations on output stream, but then you need to have correct evaluation of its full size before it's written.

If you do your calculations in a separate thread (recommended in most cases), you would need to notify the UI with progress bar update not directly, but in the UI thread, using Dispatcher.Invoke or Dispatcher.BeginInvoke. Please see my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

—SA

这篇关于添加进度条到cryptostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆