Last active
December 14, 2015 07:39
-
-
Save tupunco/c7a514525e0ffba02897 to your computer and use it in GitHub Desktop.
C# 同步/异步执行 控制台 命令
This file contains hidden or 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
/// <summary> | |
/// Async Execute Command Action | |
/// </summary> | |
/// <param name="startFileName"></param> | |
/// <param name="startFileArg"></param> | |
/// <param name="msgAction"></param> | |
/// <returns></returns> | |
public static Task<bool> ExecuteActionAsync(string startFileName, string startFileArg, Action<string> msgAction) | |
{ | |
ThrowHelper.ThrowIfNull(msgAction, "msgAction"); | |
ThrowHelper.ThrowIfNull(startFileName, "startFileName"); | |
ThrowHelper.ThrowIfNull(startFileArg, "startFileArg"); | |
var cmdProcess = new Process(); | |
var si = cmdProcess.StartInfo; | |
si.FileName = startFileName; // 命令 | |
si.Arguments = startFileArg; // 参数 | |
si.CreateNoWindow = true; // 不创建新窗口 | |
si.UseShellExecute = false; | |
si.RedirectStandardInput = true; // 重定向输入 | |
si.RedirectStandardOutput = true; // 重定向标准输出 | |
si.RedirectStandardError = true; // 重定向错误输出 | |
//si.WindowStyle = ProcessWindowStyle.Hidden; | |
var tcs = new TaskCompletionSource<bool>(); | |
cmdProcess.OutputDataReceived += (sender, e) => msgAction(e.Data); | |
cmdProcess.ErrorDataReceived += (sender, e) => msgAction(e.Data); | |
cmdProcess.EnableRaisingEvents = true; // 启用Exited事件 | |
cmdProcess.Exited += (sender, e) => tcs.SetResult(true); | |
cmdProcess.Start(); | |
cmdProcess.BeginOutputReadLine(); | |
cmdProcess.BeginErrorReadLine(); | |
return tcs.Task.ContinueWith<bool>(t => | |
{ | |
cmdProcess.Close(); | |
cmdProcess.Dispose(); | |
cmdProcess = null; | |
return true; | |
}); | |
} | |
/// <summary> | |
/// Execute Command Action | |
/// </summary> | |
/// <param name="startFileName"></param> | |
/// <param name="startFileArg"></param> | |
/// <param name="msgAction"></param> | |
/// <returns></returns> | |
public static bool ExecuteAction(string startFileName, string startFileArg, Action<string> msgAction) | |
{ | |
ThrowHelper.ThrowIfNull(msgAction, "msgAction"); | |
ThrowHelper.ThrowIfNull(startFileName, "startFileName"); | |
ThrowHelper.ThrowIfNull(startFileArg, "startFileArg"); | |
var cmdProcess = new Process(); | |
var si = cmdProcess.StartInfo; | |
si.FileName = startFileName; // 命令 | |
si.Arguments = startFileArg; // 参数 | |
si.CreateNoWindow = true; // 不创建新窗口 | |
si.UseShellExecute = false; | |
si.RedirectStandardInput = true; // 重定向输入 | |
si.RedirectStandardOutput = true; // 重定向标准输出 | |
si.RedirectStandardError = true; // 重定向错误输出 | |
//si.WindowStyle = ProcessWindowStyle.Hidden; | |
cmdProcess.OutputDataReceived += (sender, e) => msgAction(e.Data); | |
cmdProcess.ErrorDataReceived += (sender, e) => msgAction(e.Data); | |
cmdProcess.EnableRaisingEvents = true; // 启用Exited事件 | |
cmdProcess.Exited += (sender, e) => | |
{ | |
if (cmdProcess == null) | |
return; | |
cmdProcess.Close(); | |
cmdProcess.Dispose(); | |
cmdProcess = null; | |
}; | |
cmdProcess.Start(); | |
cmdProcess.BeginOutputReadLine(); | |
cmdProcess.BeginErrorReadLine(); | |
cmdProcess.WaitForExit(); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment