2016-07-28 40 views
0

有一種方法可以防止用戶在他的過程中關閉7za.exe窗口?我需要顯示文件夾提取的進展,但如果用戶關閉窗口,這可能會導致我的C#程序中出現一些錯誤。避免用戶關閉7za.exe程序,直到進程完成

ExtractForm extractForm = new ExtractForm(); 
extractForm.Show(); 

Process zipProcess = new Process(); 
     using (zipProcess) 
     { 
      zipProcess.StartInfo.UseShellExecute = false;   //Show the cmd. 
      zipProcess.StartInfo.RedirectStandardOutput = true; 
      zipProcess.OutputDataReceived += (object sender, DataReceivedEventArgs outline) => 
      { 
       LogFileExtract(outline.Data); 
       // Add args to a TextBox, ListBox, or other UI element 
      }; 
      zipProcess.StartInfo.CreateNoWindow = true; 
      zipProcess.StartInfo.FileName = pathToZip; 
      zipProcess.StartInfo.Arguments = args; 
      zipProcess.Start(); 
      zipProcess.BeginOutputReadLine(); 
      zipProcess.WaitForExit(); //Wait the process to finish completely. 

     } 
     extractForm.Close(); 
    } 
+0

http://stackoverflow.com/questions/5377423/hide-console-window-from-process-start-c-sharp這是否給你一個答案? – Will

+0

不,我想要控制檯窗口,但我不希望用戶自己關閉控制檯窗口 –

+0

然後,您可能是SOL。 – Will

回答

1

沒有直接的辦法,以防止外部窗口,該控制檯窗口的關閉,即使你開始吧:

public partial class ExtractForm : Form 
{ 
    public ExtractForm() 
    { 
     InitializeComponent(); 
    } 

    private void ExtractForm_Load(object sender, EventArgs e) 
    { 
     InitializeEvent(); 
    } 

    private void InitializeEvent() 
    { 
     Zip.LogFileExtract +=WriteExtractProgression; 
    } 

    private void WriteExtractProgression(string text) 
    { 
     if (InvokeRequired) 
     { 
      this.BeginInvoke(new Action<string>(WriteExtractProgression), text); 
     } 
     else 
     { 
      txtExtract.Text += text; 
      txtExtract.SelectionStart = txtExtract.TextLength; 
      txtExtract.ScrollToCaret(); 
      txtExtract.Refresh(); 
     } 
    } 
} 

的工藝方法。

對於這個特定的使用情況,您可以捕獲過程的輸出,你開始使用類似:

process.StartInfo.UseShellExecute = false; 
process.StartInfo.RedirectStandardOutput = true; 
process.OutputDataReceived += (sender, args) => 
{ 
    // Add args to a TextBox, ListBox, or other UI element 
} 
process.Start(); 
process.BeginOutputReadLine(); 

這將使你在UI元素直接控制。作爲獎勵,在應用程序運行時,控制檯窗口不會丟失。

+0

是的,但是這個過程可能會持續幾分鐘,如果我沒有顯示控制檯窗口,客戶會問他是否出現問題 –

+0

真的,客戶不會在你看到窗口中的輸出結果控制?如果你喜歡,你甚至可以讓TextBox看起來像一個控制檯窗口。 –

+0

最好的做法是製作一個進度條,但我不認爲我可以使用7za程序。使用你的代碼,我可以獲得進展,並像我看到的那樣寫入我的winform。這是我無法做到的最好的事情。謝謝:) –