mirror of
https://github.com/sshnet/SSH.NET.git
synced 2026-10-19 06:58:56 +08:00
Improve thread-safe event handling
Replace Task.Factory.StartNew with ThreadPool.QueueUserWorkItem
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(); });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user