Improve thread-safe event handling

Replace Task.Factory.StartNew with ThreadPool.QueueUserWorkItem
This commit is contained in:
olegkap_cp
2013-04-04 15:33:47 +00:00
parent 908c22cc93
commit e189418a5b
15 changed files with 41 additions and 23 deletions
+6 -4
View File
@@ -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);
}
}
@@ -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)
@@ -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(); });
}
}
}
@@ -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,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(); });
}
}
}
@@ -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(); });
}
}
}
@@ -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(); });
}
}
}
@@ -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
View File
@@ -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);
}
}
@@ -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
View File
@@ -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
View File
@@ -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);
}
}
@@ -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
View File
@@ -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));
}
}
}
}
@@ -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(); });
}
}
}