mirror of
https://github.com/ShareX/ShareX.git
synced 2024-07-15 19:36:38 +12:00
94 lines
2.5 KiB
C#
94 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace HelpersLib
|
|
{
|
|
public abstract class ExternalCLIManager : IDisposable
|
|
{
|
|
private Process CLI = new Process();
|
|
|
|
public StringBuilder Output = new StringBuilder();
|
|
public StringBuilder Errors = new StringBuilder();
|
|
|
|
public delegate void ErrorDataReceivedHandler();
|
|
public event ErrorDataReceivedHandler ErrorDataReceived;
|
|
|
|
public virtual void Open(string cliPath)
|
|
{
|
|
ProcessStartInfo psi = new ProcessStartInfo(cliPath);
|
|
psi.UseShellExecute = false;
|
|
psi.ErrorDialog = false;
|
|
psi.RedirectStandardInput = true;
|
|
psi.RedirectStandardError = true;
|
|
psi.RedirectStandardOutput = true;
|
|
psi.WorkingDirectory = Path.GetDirectoryName(cliPath);
|
|
|
|
psi.WindowStyle = ProcessWindowStyle.Normal;
|
|
|
|
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
|
|
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
|
|
{
|
|
CLI.OutputDataReceived += (sender, e) =>
|
|
{
|
|
if (e.Data == null)
|
|
{
|
|
outputWaitHandle.Set();
|
|
}
|
|
else
|
|
{
|
|
Output.AppendLine(e.Data);
|
|
}
|
|
};
|
|
|
|
CLI.ErrorDataReceived += (sender, e) =>
|
|
{
|
|
if (e.Data == null)
|
|
{
|
|
errorWaitHandle.Set();
|
|
}
|
|
else
|
|
{
|
|
Errors.AppendLine(e.Data);
|
|
}
|
|
};
|
|
|
|
CLI.StartInfo = psi;
|
|
CLI.Start();
|
|
}
|
|
}
|
|
|
|
public void SendCommand(string command)
|
|
{
|
|
if (CLI != null)
|
|
{
|
|
CLI.StandardInput.WriteLine(command);
|
|
}
|
|
}
|
|
|
|
public virtual void Close()
|
|
{
|
|
CLI.CloseMainWindow();
|
|
}
|
|
|
|
public virtual void OnErrorDataReceived()
|
|
{
|
|
if (ErrorDataReceived != null)
|
|
{
|
|
ErrorDataReceived();
|
|
}
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
if (CLI != null)
|
|
{
|
|
CLI.Dispose();
|
|
}
|
|
}
|
|
}
|
|
} |