2014-04-14 3 views
0

저는 Minecraft 서버의 관리자 응용 프로그램에서 작업 중이며 프로그램을 실행하면 콘솔이 표시되고 사라집니다. 수동으로 실행하면 문제없이 실행되고 문제가 발생합니다. 내 코드 :실행중인 배치 파일

Process process = new Process 
      { 
       StartInfo = 
       { 
        FileName = textBox2.Text, 
        //Arguments = textBox3.Text, 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = false, 
       } 
      }; 

process.OutputDataReceived += new DataReceivedEventHandler(server_outputDataReceived); 
server = process; 
process.Start(); 

배치 파일 코드 (배치 파일이 무엇인지 언어 나도 몰라, 그래서 나는 기본적으로 하나의 사용 - 언어 선택) : BTW

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false 

합니다. 파일을 만들지 않고 프로세스를 시작할 수 있습니까? (예 : 파일을 만들지 않고 "java -jar example"프로세스 시작)? 세 번째 질문에

@Edit 답변 : Answer to the third question

내 전체 코드 (메시지 상자가 폴란드에서 메신저 렸기 때문에, 폴란드어,하지만 나중에 내가 다른 언어에 대한 지원을 추가합니다) :

using System; 
using System.IO; 
using System.Windows.Forms; 
using System.Diagnostics; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     public Process server; 

     private Boolean runServer() 
     { 
      if (!File.Exists(textBox2.Text)) 
      { 
       MessageBox.Show("Brak określonej ścieżki dostępu! (" + textBox2.Text + ")", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return false; 
      } 

      Process process = new Process 
      { 
       StartInfo = 
       { 
        FileName = textBox2.Text, 
        //Arguments = textBox3.Text, 
        UseShellExecute = false, 
        RedirectStandardOutput = true, 
        CreateNoWindow = false, 
       } 
      }; 

      process.OutputDataReceived += new DataReceivedEventHandler(server_outputDataReceived); 
      process.ErrorDataReceived += new DataReceivedEventHandler(server_outputDataReceived); 
      server = process; 

      if (process.Start()) 
       return true; 
      else 
      { 
       MessageBox.Show("Nie można włączyć serwera!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       return false; 
      } 
     } 

     private String ReadFile(String filename, int line) 
     { 
      StreamReader reader = new StreamReader(filename); 

      for (int i = 0; i < line; i++) 
      { 
       reader.ReadLine(); 
      } 

      return reader.ReadLine(); 
     } 

     private void ReloadOPs() 
     { 
      if (!File.Exists(textBox1.Text)) 
      { 
       MessageBox.Show("Sciezka dostępu do pliku z listą graczy posiadających OP nie istnieje! (" + textBox1.Text + ")", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
       tabControl1.SelectedTab = tabPageOptions; 
       textBox1.SelectAll(); 
       return; 
      } 

      String line = ReadFile(textBox1.Text, 0); 
      comboBox1.Items.Clear(); 
      for (int i = 1; i < File.ReadAllLines(textBox1.Text).Length; i++) 
      { 
       if (!String.IsNullOrWhiteSpace(ReadFile(textBox1.Text, i))) 
       { 
        comboBox1.Items.Add(line); 
        line = ReadFile(textBox1.Text, i); 
       } 
      } 

      MessageBox.Show("Lista graczy z OP, została odświeżona."); 
     } 

     // OPs combobox (OPs) 
     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      groupBox1.Text = comboBox1.SelectedItem.ToString(); 
      groupBox1.Visible = true; 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      textBox1.Text = Application.StartupPath.ToString() + @"\ops.txt"; 
      ReloadOPs(); 
     } 

     // Reload OPs button (OPs) 
     private void button1_Click(object sender, EventArgs e) 
     { 
      ReloadOPs(); 
     } 


     // Save button (Options) 
     private void button4_Click(object sender, EventArgs e) 
     { 

     } 

     private void server_outputDataReceived(object sender, DataReceivedEventArgs e) 
     { 
      addConsoleMessage(e.Data.ToString(), true); 
     } 

     // Run server button (Menu) 
     private void button5_Click(object sender, EventArgs e) 
     { 
      if (!runServer()) 
       return; 

      server.BeginOutputReadLine(); 
      button6.Enabled = true; 
     } 

     // Stop server button (Menu) 
     private void button6_Click(object sender, EventArgs e) 
     { 
      if(!server.HasExited) 
       server.Kill(); 
      button6.Enabled = false; 
     } 

     private void addConsoleMessage(String message, Boolean refresh) 
     { 
      listBox1.Items.Add(message); 
      if (refresh) 
       listBox1.Refresh(); 
     } 
    } 
} 

지금 작동하지만 배치 파일이 반환 된 두 번째 메시지 (?)가 지나면 InvaildOperationException 처리되지 않았기 때문에 프로그램이 충돌합니다.

+0

어? 콘솔에 아무것도 표시되지 않는 유일한 문제입니까? 콘솔에 입력을 기다리거나 아무것도 표시하지 않는 것은 여기에 없습니다. 무엇이 "수동으로 실행"하시겠습니까? – tnw

+0

이 코드를 디버깅 해본 적이 있습니까? 아마도 예외가 발생하고 내부의 'Exit (0)'또는 뭔가를 시도하고 디버깅을 시도하고 다시보고하십시오. – MethodMan

+0

아니요, 콘솔이 닫히고 있습니다. . 수동으로 실행 - 배치 파일 코드 (start.bat)가있는 수동 bat 파일을 실행합니다. – Mibac

답변

0

확인. 몇 가지 점을 분명히 해두 자. 난 당신이 콘텐츠와 filename.bat라는 배치 파일이 있다고 가정 :

java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false 

와와

는 "수동으로 실행을"당신이 명령 줄 창을 열고 다음을 입력 의미 : filename을. 승인? 그러나 수동으로 실행하지 않을 때 배치 파일을 어떻게 실행합니까? 이다 start.bat start 때문에 배치 파일의 이름은 안됩니다 -

cd "%~P0" 
java -Xmx1024M -jar craftbukkit-1.7.2-R0.3.jar -o false 

PS가 : 파일 브라우저에서 더블 클릭하면, 실종 배치 파일은 배치 파일 인 하나 현재 디렉토리를 변경하려면 내부 cmd.exe 명령의 이름!

+0

지금 작동하지만 배치 파일이 반환 된 두 번째 메시지 (?)가 지나면 InvaildOperationException 처리되지 않았기 때문에 프로그램이 충돌합니다. – Mibac