【问题标题】:My Stopwatch is not keeping and displaying time我的秒表没有保持和显示时间
【发布时间】:2014-11-03 04:04:51
【问题描述】:

我在 C# 中编写了一个程序,假设计算在 RSA 中加密和解密需要多长时间,然后显示时间。我将秒表对象放入加密和解密方法中,而不是创建自己的方法。无论我尝试过什么,输出始终是 00:00:00:00。

public void EncryptFile(string inFile)
{
   Stopwatch stopWatch = new Stopwatch();
   // Get the elapsed time as a TimeSpan value.
   TimeSpan ts = stopWatch.Elapsed;
   stopWatch.Start();

   // Create instance of Rijndael for 
   // symetric encryption of the data.

   RijndaelManaged rjndl = new RijndaelManaged();
   rjndl.KeySize = 256;
   rjndl.BlockSize = 256;
   rjndl.Mode = CipherMode.CBC;
   ICryptoTransform transform = rjndl.CreateEncryptor();

   // Use RSACryptoServiceProvider to 
   // enrypt the Rijndael key. 
   // rsa is previously instantiated:  
   // rsa = new RSACryptoServiceProvider(cspp); 
   byte[] keyEncrypted = rsa.Encrypt(rjndl.Key, false);

   // Create byte arrays to contain 
   // the length values of the key and IV. 
   byte[] LenK = new byte[4];
   byte[] LenIV = new byte[4];

   int lKey = keyEncrypted.Length;
   LenK = BitConverter.GetBytes(lKey);
   int lIV = rjndl.IV.Length;
   LenIV = BitConverter.GetBytes(lIV);

   // Write the following to the FileStream 
   // for the encrypted file (outFs): 
   // - length of the key 
   // - length of the IV 
   // - ecrypted key 
   // - the IV 
   // - the encrypted cipher content 

   int startFileName = inFile.LastIndexOf("\\") + 1;
   // Change the file's extension to ".enc" 
   string outFile = EncrFolder +
      inFile.Substring(startFileName,
      inFile.LastIndexOf(".") -      startFileName) + ".enc";

   using (FileStream outFs = new FileStream(outFile, FileMode.Create))
   {
      outFs.Write(LenK, 0, 4);
      outFs.Write(LenIV, 0, 4);
      outFs.Write(keyEncrypted, 0, lKey);
      outFs.Write(rjndl.IV, 0, lIV);

      // Now write the cipher text using 
      // a CryptoStream for encrypting. 
      using (CryptoStream outStreamEncrypted =
         new CryptoStream(outFs, transform, CryptoStreamMode.Write))
      {
         int count = 0;
         int offset = 0;

         // blockSizeBytes can be any arbitrary size. 
         int blockSizeBytes = rjndl.BlockSize / 8;
         byte[] data = new byte[blockSizeBytes];
         int bytesRead = 0;

         using (FileStream inFs = new FileStream(inFile, FileMode.Open))
         {
            do
            {
               count = inFs.Read(data, 0, blockSizeBytes);
               offset += count;
               outStreamEncrypted.Write(data, 0, count);
               bytesRead += blockSizeBytes;
            } while (count > 0);

            inFs.Close();
         }
         outStreamEncrypted.FlushFinalBlock();
         stopWatch.Stop();
         // Format and display the TimeSpan value. 
         string elapsedTime = string.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
         label3.Text = "Elapsed time " + elapsedTime;
         outStreamEncrypted.Close();
      }
      outFs.Close();
   }
}

【问题讨论】:

  • 请注意,这个问题实际上与加密无关,因此不应使用它来标记;还有很多与时间无关的代码可以在发布之前删除。

标签: c# time stopwatch


【解决方案1】:

你在开始计时之前记录了经过的时间(所以它显然是零):

TimeSpan ts = stopWatch.Elapsed;
stopWatch.Start();

您在停止计时后引用ts,但您永远不会更新ts 的值。

stopWatch.Stop();
// Format and display the TimeSpan value. 
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);

相反,您需要检查停止计时后经过的时间 - 将ts 的声明和初始化移到Stop 之后:

stopWatch.Stop();
// Format and display the TimeSpan value. 
TimeSpan ts = stopWatch.Elapsed;
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
    ts.Hours, ts.Minutes, ts.Seconds,
    ts.Milliseconds / 10);

【讨论】:

    【解决方案2】:

    TimeSpan 是一种值类型,您不能只分配TimeSpan ts = stopWatch.Elapsed; 并期望它发生变化。使用 stopWatch.Elapsed 而不是将其分配给时间卡住的变量。

    【讨论】:

      【解决方案3】:

      看起来您在 string.Format 方法中使用了变量 ts,但在调用 stopWatch.Stop() 之后,它看起来并没有设置为 stopWatch.ElapsedTime

      【讨论】:

        【解决方案4】:

        在停止计时器后记录经过的时间。

        stopWatch.Start()
        // Your code Here 
        stopWatch.Stop()
        TimeSpan ts = stopWatch.Elapsed
        

        并且可以使用

        ts.Hours,
        ts.Minutes, 
        ts.Seconds,
        ts.Milliseconds
        ts.Ticks
        

        小时、分钟、秒、毫秒和刻度

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-06-15
          • 2020-06-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多