Explorar o código

Improve thread-safe event handling
Replace Task.Factory.StartNew with ThreadPool.QueueUserWorkItem

olegkap_cp %!s(int64=12) %!d(string=hai) anos
pai
achega
e189418a5b

+ 6 - 4
Renci.SshClient/Renci.SshNet/BaseClient.cs

@@ -182,17 +182,19 @@ namespace Renci.SshNet
 
          private void Session_ErrorOccured(object sender, ExceptionEventArgs e)
         {
-            if (this.ErrorOccurred != null)
+            var handler = this.ErrorOccurred;
+            if (handler != null)
             {
-                this.ErrorOccurred(this, e);
+                handler(this, e);
             }
         }
 
         private void Session_HostKeyReceived(object sender, HostKeyEventArgs e)
         {
-            if (this.HostKeyReceived != null)
+            var handler = this.HostKeyReceived;
+            if (handler != null)
             {
-                this.HostKeyReceived(this, e);
+                handler(this, e);
             }
         }
 

+ 2 - 1
Renci.SshClient/Renci.SshNet/Channels/ChannelDirectTcpip.NET40.cs

@@ -1,6 +1,7 @@
 using System.Threading.Tasks;
 using System;
 using System.Net.Sockets;
+using System.Threading;
 
 namespace Renci.SshNet.Channels
 {
@@ -11,7 +12,7 @@ namespace Renci.SshNet.Channels
     {
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
 
         partial void InternalSocketReceive(byte[] buffer, ref int read)

+ 2 - 1
Renci.SshClient/Renci.SshNet/ForwardedPortDynamic.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -7,7 +8,7 @@ namespace Renci.SshNet
     {
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 2 - 1
Renci.SshClient/Renci.SshNet/ForwardedPortLocal.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -10,7 +11,7 @@ namespace Renci.SshNet
     {
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 2 - 1
Renci.SshClient/Renci.SshNet/ForwardedPortRemote.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -10,7 +11,7 @@ namespace Renci.SshNet
     {
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 1 - 1
Renci.SshClient/Renci.SshNet/KeyboardInteractiveAuthenticationMethod.NET40.cs

@@ -15,7 +15,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 1 - 1
Renci.SshClient/Renci.SshNet/PasswordAuthenticationMethod.NET40.cs

@@ -15,7 +15,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 1 - 1
Renci.SshClient/Renci.SshNet/Session.NET40.cs

@@ -22,7 +22,7 @@ namespace Renci.SshNet
 
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action, TaskCreationOptions.LongRunning);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
 
         partial void InternalRegisterMessage(string messageName)

+ 3 - 2
Renci.SshClient/Renci.SshNet/Session.cs

@@ -1481,9 +1481,10 @@ namespace Renci.SshNet
 
         private void KeyExchange_HostKeyReceived(object sender, HostKeyEventArgs e)
         {
-            if (this.HostKeyReceived != null)
+            var handler = this.HostKeyReceived;
+            if (handler != null)
             {
-                this.HostKeyReceived(this, e);
+                handler(this, e);
             }
         }
 

+ 2 - 1
Renci.SshClient/Renci.SshNet/SftpClient.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -15,7 +16,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name="action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 2 - 1
Renci.SshClient/Renci.SshNet/Shell.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -11,7 +12,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name=" action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 3 - 2
Renci.SshClient/Renci.SshNet/Shell.cs

@@ -212,9 +212,10 @@ namespace Renci.SshNet
 
         private void RaiseError(ExceptionEventArgs e)
         {
-            if (this.ErrorOccurred != null)
+            var handler = this.ErrorOccurred;
+            if (handler != null)
             {
-                this.ErrorOccurred(this, e);
+                handler(this, e);
             }
         }
 

+ 2 - 1
Renci.SshClient/Renci.SshNet/ShellStream.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -11,7 +12,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name=" action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }

+ 10 - 4
Renci.SshClient/Renci.SshNet/ShellStream.cs

@@ -740,14 +740,20 @@ namespace Renci.SshNet
 
         private void OnRaiseError(ExceptionEventArgs e)
         {
-            if (this.ErrorOccurred != null)
-                this.ErrorOccurred(this, e);
+            var handler = this.ErrorOccurred;
+            if (handler != null)
+            {
+                handler(this, e);
+            }
         }
 
         private void OnDataReceived(byte[] data)
         {
-            if (this.DataReceived != null)
-                this.DataReceived(this, new ShellDataEventArgs(data));
+            var handler = this.DataReceived;
+            if (handler != null)
+            {
+                handler(this, new ShellDataEventArgs(data));
+            }
         }
     }
 }

+ 2 - 1
Renci.SshClient/Renci.SshNet/SshCommand.NET40.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Threading;
 using System.Threading.Tasks;
 
 namespace Renci.SshNet
@@ -11,7 +12,7 @@ namespace Renci.SshNet
         /// <exception cref="ArgumentNullException"><paramref name=" action"/> is null.</exception>
         partial void ExecuteThread(Action action)
         {
-            Task.Factory.StartNew(action);
+            ThreadPool.QueueUserWorkItem((o) => { action(); });
         }
     }
 }