forked from zsh2401/AutumnBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
现在,LeafExtension可以申请分配ICommandExecutor了,这个版本有个严重bug:在LeafUI结束后,无规律出现N…
…ull异常,导致秋之盒完全崩溃
- Loading branch information
Showing
17 changed files
with
287 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
AutumnBox.Basic.Shared/Calling/CommandExecutorExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Basic.Calling | ||
{ | ||
public static class CommandExecutorExtension | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Management; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AutumnBox.Basic.Data; | ||
|
||
namespace AutumnBox.Basic.Calling | ||
{ | ||
public class HestExecutor : ICommandExecutor | ||
{ | ||
private class HestExecutorResult : ICommandResult | ||
{ | ||
public int ExitCode { get; set; } = 0; | ||
|
||
public Output Output { get; set; } = null; | ||
} | ||
public event EventHandler Disposed; | ||
public event EventHandler<CommandExecutingEventArgs> CommandExecuting; | ||
public event EventHandler<CommandExecutedEventArgs> CommandExecuted; | ||
public event EventHandler<OutputReceivedEventArgs> OutputReceived; | ||
private Process currentProcess; | ||
private readonly OutputBuilder outputBuilder = new OutputBuilder(); | ||
private ProcessStartInfo GetStartInfo(string fileName, string args) | ||
{ | ||
return new ProcessStartInfo() | ||
{ | ||
FileName = fileName, | ||
Arguments = args, | ||
UseShellExecute = false, | ||
CreateNoWindow = true, | ||
RedirectStandardError = true, | ||
RedirectStandardOutput = true | ||
}; | ||
} | ||
private readonly object _executingLock = new object(); | ||
|
||
public ICommandResult Execute(string fileName, string args) | ||
{ | ||
lock (_executingLock) | ||
{ | ||
if (isDisposed) throw new ObjectDisposedException(nameof(HestExecutor)); | ||
//输出构造器 | ||
outputBuilder.Clear(); | ||
//记录开始时间 | ||
DateTime start = DateTime.Now; | ||
//开始进程 | ||
currentProcess = Process.Start(GetStartInfo(fileName, args)); | ||
|
||
//监听 | ||
currentProcess.OutputDataReceived += (s, e) => | ||
{ | ||
outputBuilder.AppendOut(e.Data); | ||
OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, false)); | ||
}; | ||
currentProcess.ErrorDataReceived += (s, e) => | ||
{ | ||
outputBuilder.AppendError(e.Data); | ||
OutputReceived?.Invoke(this, new OutputReceivedEventArgs(e, true)); | ||
}; | ||
currentProcess.BeginOutputReadLine(); | ||
currentProcess.BeginErrorReadLine(); | ||
|
||
//触发事件 | ||
CommandExecuting?.Invoke(this, new CommandExecutingEventArgs(fileName, args)); | ||
|
||
//等待进程结束 | ||
currentProcess.WaitForExit(); | ||
currentProcess.CancelErrorRead(); | ||
currentProcess.CancelOutputRead(); | ||
|
||
//记录结束时间 | ||
DateTime end = DateTime.Now; | ||
//构造结果对象 | ||
var result = new HestExecutorResult() { ExitCode = currentProcess.ExitCode, Output = outputBuilder.Result }; | ||
//触发结束事件 | ||
CommandExecuted?.Invoke(this, new CommandExecutedEventArgs(fileName, args, result, end - start)); | ||
//返回结果 | ||
return result; | ||
}; | ||
} | ||
|
||
private bool isDisposed = false; | ||
|
||
public void Dispose() | ||
{ | ||
isDisposed = true; | ||
CancelCurrent(); | ||
} | ||
|
||
public void CancelCurrent() | ||
{ | ||
currentProcess?.KillByTaskKill(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using AutumnBox.Basic.Data; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutumnBox.Basic.Calling | ||
{ | ||
/// <summary> | ||
/// 标准的CommandExecutor命令执行器 | ||
/// </summary> | ||
public interface ICommandExecutor : IDisposable | ||
{ | ||
/// <summary> | ||
/// 当CommandExecutor被析构时触发 | ||
/// </summary> | ||
event EventHandler Disposed; | ||
/// <summary> | ||
/// 当一条命令开始执行时触发 | ||
/// </summary> | ||
event EventHandler<CommandExecutingEventArgs> CommandExecuting; | ||
/// <summary> | ||
/// 当一条命令完成时触发 | ||
/// </summary> | ||
event EventHandler<CommandExecutedEventArgs> CommandExecuted; | ||
/// <summary> | ||
/// 接收到输出时触发的事件 | ||
/// </summary> | ||
event EventHandler<OutputReceivedEventArgs> OutputReceived; | ||
/// <summary> | ||
/// 执行命令 | ||
/// </summary> | ||
/// <param name="fileName"></param> | ||
/// <param name="args"></param> | ||
/// <returns></returns> | ||
ICommandResult Execute(string fileName, string args); | ||
/// <summary> | ||
/// 取消执行当前执行的命令 | ||
/// </summary> | ||
void CancelCurrent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using AutumnBox.Basic.Calling; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Basic.Data | ||
{ | ||
public class CommandExecutedEventArgs : EventArgs | ||
{ | ||
TimeSpan UsedTime { get; } | ||
ICommandResult Result { get; } | ||
public string FileName { get; } | ||
public string Args { get; } | ||
public TimeSpan Span { get; } | ||
|
||
public CommandExecutedEventArgs(string fileName, string args, ICommandResult result, TimeSpan span) | ||
{ | ||
if (string.IsNullOrEmpty(fileName)) | ||
{ | ||
throw new ArgumentException("message", nameof(fileName)); | ||
} | ||
|
||
FileName = fileName; | ||
Args = args; | ||
Span = span; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace AutumnBox.Basic.Data | ||
{ | ||
public class CommandExecutingEventArgs : EventArgs | ||
{ | ||
public string FileName { get; } | ||
public string[] Args { get; } | ||
public CommandExecutingEventArgs(string fileName, params string[] args) | ||
{ | ||
FileName = fileName; | ||
Args = args; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.