Earl Switzer
Earl Switzer

Reputation: 1

My Stopwatch is not keeping and displaying time

I put together a program in C# that is suppose to time how long it takes to encrypt and decrypt in RSA and then display the time. I put the stopwatch object into the encrypt and decrypt method instead of making a method of its own. No matter what I have tried, the output is always 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();
   }
}

Upvotes: 0

Views: 253

Answers (4)

Ajay Unnikrishnan
Ajay Unnikrishnan

Reputation: 129

Take elapsed time after stop timer.

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

and can use

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

for hour, minute, second, millisecond and Ticks

Upvotes: 1

jt000
jt000

Reputation: 3236

Looks like you're using the variable ts in your string.Format method, but it doesn't look like its ever set to stopWatch.ElapsedTime after calling stopWatch.Stop().

Upvotes: 1

Tyress
Tyress

Reputation: 3653

TimeSpan is a value type, you can't just assign TimeSpan ts = stopWatch.Elapsed; and expect it to change. Use the stopWatch.Elapsed instead of assigning it to a variable that is stuck in time.

Upvotes: 1

Blorgbeard
Blorgbeard

Reputation: 103467

You record the elapsed time before you start timing (so it's obviously zero):

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

You then refer to ts after you stop timing, but you never update the value of 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);

Instead, you need to check the elapsed time after you stop timing - move the declaration and initialization of ts down to after the 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);

Upvotes: 4

Related Questions