2014-04-15 2 views
0

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

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

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

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이 처리되지 않았기 때문에 (addConsoleMessage의 listBox1.Items.Add (message)). 외부 오류 정보 : 스레드 간의 연산이 잘못되었습니다. 'listBox1'컨트롤은 스레드가 작성된 스레드 이외의 스레드에서 액세스됩니다.

+1

중단 점을 설정하고'addConsoleMessage' 함수를 디버그하고 실제로 예외를 발생시키는 원인을 확인하십시오. –

+0

그 이유는 InvalidOperationException이 발생합니다. "스레드간에 잘못된 연산이 발생했습니다. 'listBox1'컨트롤이 작성된 스레드 이외의 스레드에서 액세스됩니다." – Mibac

답변

2

UI 양식 백그라운드 스레드를 업데이트 할 수 없습니다. 당신이 System.Windows.Forms.Control의 문서에서 볼 수 있듯이 Invoke/BeginInvoke 방법은 컨트롤 개체에 직접적으로 윈폼에서이

WPF

private void server_outputDataReceived(object sender, DataReceivedEventArgs e) 
    { 
     Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Normal,() => 
       { 
        addConsoleMessage(e.Data.ToString(), true); 
       }); 
    } 

업데이트

을보십시오. 예를 들어 listBox1.BeginInvoke (...)를 사용할 수 있습니다.

+0

'Threading'형식이 네임 스페이스 'System.Windows'에 없습니다 (어셈블리 참조가 누락 되었습니까?) – Mibac

+0

이 질문은 WPF를 대상으로하는 질문에 대답하는 것으로 보입니다. 그러나 OP는 WinForm의''partial class : Form'. – Habib

+0

@Habib 감사합니다. 내 대답을 업데이트했습니다. –