我提供了Windows服务,该服务是控制台应用程序的包装。在功能上,它可以正常运行,并以有状态的方式启动和停止应用。我注意到的唯一问题是该服务将以100%运行1个核心。在VS2019调试模式下运行它时似乎并没有旋转CPU,因此我无法通过尝试调试代码来了解原因。它是使用Topshelf创建Windows服务的非常简单的程序。我在此处包含程序的两个文件:
class Program
{
static void Main(string[] args)
{
var rc = HostFactory.Run(x =>
{
x.Service<BedrockServiceWrapper>(s =>
{
s.ConstructUsing(name => new BedrockServiceWrapper());
s.WhenStarted(tc => tc.Start());
s.WhenStopped(tc => tc.Stop());
});
x.RunAsNetworkService();
x.SetDescription("Windows Service Wrapper for Windows Bedrock Server");
x.SetDisplayName("BedrockService");
x.SetServiceName("BedrockService");
});
var exitCode = (int)Convert.ChangeType(rc, rc.GetTypeCode());
Environment.ExitCode = exitCode;
}
}
和
public class BedrockServiceWrapper
{
Process process;
Thread outputThread;
Thread errorThread;
Thread inputThread;
static BackgroundWorker bedrockServer;
string exePath;
public BedrockServiceWrapper()
{
exePath = ConfigurationManager.AppSettings["BedrockServerExeLocation"];
bedrockServer = new BackgroundWorker
{
WorkerSupportsCancellation = true
};
}
public void Stop()
{
process.StandardInput.WriteLine("stop");
bedrockServer.CancelAsync();
}
public void Start()
{
bedrockServer.DoWork += (s, e) =>
{
RunServer(exePath);
};
bedrockServer.RunWorkerAsync();
}
public void RunServer(string path)
{
// Fires up a new process to run inside this one
process = Process.Start(new ProcessStartInfo
{
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = path
});
// Depending on your application you may either prioritize the IO or the exact opposite
const ThreadPriority ioPriority = ThreadPriority.Highest;
outputThread = new Thread(outputReader) { Name = "ChildIO Output", Priority = ioPriority };
errorThread = new Thread(errorReader) { Name = "ChildIO Error", Priority = ioPriority };
inputThread = new Thread(inputReader) { Name = "ChildIO Input", Priority = ioPriority };
// Set as background threads (will automatically stop when application ends)
outputThread.IsBackground = errorThread.IsBackground
= inputThread.IsBackground = true;
// Start the IO threads
outputThread.Start(process);
errorThread.Start(process);
inputThread.Start(process);
process.WaitForExit();
}
/// <summary>
/// Continuously copies data from one stream to the other.
/// </summary>
/// <param name="instream">The input stream.</param>
/// <param name="outstream">The output stream.</param>
private static void passThrough(Stream instream, Stream outstream)
{
byte[] buffer = new byte[4096];
while (true)
{
int len;
while ((len = instream.Read(buffer, 0, buffer.Length)) > 0)
{
outstream.Write(buffer, 0, len);
outstream.Flush();
}
}
}
private static void outputReader(object p)
{
var process = (Process)p;
// Pass the standard output of the child to our standard output
passThrough(process.StandardOutput.BaseStream, Console.OpenStandardOutput());
}
private static void errorReader(object p)
{
var process = (Process)p;
// Pass the standard error of the child to our standard error
passThrough(process.StandardError.BaseStream, Console.OpenStandardError());
}
private static void inputReader(object p)
{
var process = (Process)p;
// Pass our standard input into the standard input of the child
passThrough(Console.OpenStandardInput(), process.StandardInput.BaseStream);
}
}
这与线程跨流复制输入和输出有关吗?我不相信有太多报道,但是也许它只是试图复制很多零长度的Stream块?