summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crypto/src/crypto/prng/ThreadedSeedGenerator.cs53
1 files changed, 34 insertions, 19 deletions
diff --git a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs b/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
index 0a38e5f5a..7412e4919 100644
--- a/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
+++ b/crypto/src/crypto/prng/ThreadedSeedGenerator.cs
@@ -5,6 +5,8 @@ using System.Threading;
 using System.Threading.Tasks;
 #endif
 
+using Org.BouncyCastle.Utilities;
+
 namespace Org.BouncyCastle.Crypto.Prng
 {
     /**
@@ -71,36 +73,49 @@ namespace Org.BouncyCastle.Crypto.Prng
                 ThreadPool.QueueUserWorkItem(new WaitCallback(Run));
 #endif
 
-                for (int i = 0; i < end; i++)
+#if PORTABLE
+                AutoResetEvent autoResetEvent = new AutoResetEvent(false);
+#endif
+
+                try
                 {
-                    while (this.counter == last)
+                    for (int i = 0; i < end; i++)
                     {
-                        try
+                        while (this.counter == last)
                         {
+                            try
+                            {
 #if PORTABLE
-                            new AutoResetEvent(false).WaitOne(1);
+                                autoResetEvent.WaitOne(1);
 #else
-                            Thread.Sleep(1);
+                                Thread.Sleep(1);
 #endif
+                            }
+                            catch (Exception)
+                            {
+                                // ignore
+                            }
                         }
-                        catch (Exception)
-                        {
-                            // ignore
-                        }
-                    }
 
-                    last = this.counter;
+                        last = this.counter;
 
-                    if (fast)
-                    {
-                        result[i] = (byte)last;
-                    }
-                    else
-                    {
-                        int bytepos = i / 8;
-                        result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
+                        if (fast)
+                        {
+                            result[i] = (byte)last;
+                        }
+                        else
+                        {
+                            int bytepos = i / 8;
+                            result[bytepos] = (byte)((result[bytepos] << 1) | (last & 1));
+                        }
                     }
                 }
+                finally
+                {
+#if PORTABLE
+                    autoResetEvent.Close();
+#endif
+                }
 
                 this.stop = true;