CTZServerControl/CTZServerControl/Frm_Main.cs

97 lines
2.9 KiB
C#

using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace CTZServerControl
{
public partial class Frm_Main : Form
{
Process process = null;
public Frm_Main()
{
InitializeComponent();
}
private void start_Click(object sender, EventArgs e)
{
start.Enabled = false;
ProcessStartInfo startinfo = new ProcessStartInfo(@"C:\Program Files\Java\jre7\bin\java.exe");
startinfo.Arguments = "-Xmx2048M -jar spigot.jar";
startinfo.UseShellExecute = false;
startinfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory + "Spigot";
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardError = true;
startinfo.RedirectStandardOutput = true;
startinfo.CreateNoWindow = true;
process = Process.Start(startinfo);
process.BeginErrorReadLine();
process.ErrorDataReceived += Process_OutputDataReceived;
process.BeginOutputReadLine();
process.OutputDataReceived += Process_OutputDataReceived;
process.Exited += Process_Exited;
}
private void Process_Exited(object sender, EventArgs e)
{
outdata.Items.Add("服务端已退出!");
start.Enabled = true;
}
//新建委托
private delegate void OutputDataDelegate(string number);
//委托调用的方法
private void OutputData(string data)
{
bool scroll = this.outdata.TopIndex == getListEndIndex(outdata);
outdata.Items.Add(data);
if (scroll) this.outdata.TopIndex = getListEndIndex(outdata);
}
private int getListEndIndex(ListBox listbox)
{
return listbox.Items.Count - (int)(listbox.Height / listbox.ItemHeight);
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data == null)
{
process.OutputDataReceived -= Process_OutputDataReceived;
}
else
{
Invoke(new OutputDataDelegate(OutputData), new object[] { e.Data });
}
}
private void send_Click(object sender, EventArgs e)
{
sendcmd();
}
private void Frm_Main_FormClosed(object sender, FormClosedEventArgs e)
{
process.Kill();
}
private void cmd_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
sendcmd();
}
}
private void sendcmd()
{
runcmd(cmd.Text);
cmd.Text = "";
}
private void runcmd(string cmd)
{
process.StandardInput.WriteLine(cmd);
}
}
}