2013-08-03 59 views
0

我創建了一個C#windows應用程序,我插入了進度條。進度條在按鈕上啓動和停止點擊使用線程

當單擊一個按鈕時,應該會出現進度條,然後它應該啓動進程大約2到3秒,當進程欄完成時,它應該被隱藏。

我已經使用此代碼來解決這個問題,但它不工作。

在進度欄運行時,標籤框應該類似於「正在生成... 45%」,並且在完成標籤框後應該是「生成100%..」,但是當我插入標籤時顯示一些錯誤。

這裏是點擊Generate按鈕前的畫面..

enter image description here
在處理我應該得到這樣的..
enter image description here
在最後的進程ID應該是這樣和進度條應該隱藏..
enter image description here

 ProgressBar1.Visible = true; 

     if (isProcessRunning) 
     { 
      MessageBox.Show("A process is already running."); 
      return; 
     } 

     Thread backgroundThread = new Thread(
      new ThreadStart(() => 
      { 
       isProcessRunning = true; 
       for (int n = 0; n < 100; n++) 
       { 
        Thread.Sleep(1); 
        progressBar1.BeginInvoke(new Action(() => progressBar1.Value = n)); 
       } 
       MessageBox.Show("Generated!!!"); 


       if (progressBar1.InvokeRequired) 
        progressBar1.BeginInvoke(new Action(() => progressBar1.Value = 0)); 
       isProcessRunning = false; 
      } 

     )); 

     // Start the background process thread 
     backgroundThread.Start(); 
+1

...什麼錯誤? – zey

+0

在生成完成後,我無法隱藏進度條.. – coolprarun

+1

你的意思是,100%完成後?如何將這個進度條設置爲false? – zey

回答

10

我建議你使用BackgroundWorker來顯示C#winform中的進度條。
下面是一個例子,

public partial class Form1 : Form 
{ 
    BackgroundWorker bgw = new BackgroundWorker();  
    public Form1() 
    { 
     InitializeComponent(); 
     label1.Text = ""; 
     label2.Text = ""; 
    } 

    private void button1_Click_1(object sender, EventArgs e) 
    { 
     bgw.DoWork += new DoWorkEventHandler(bgw_DoWork); 
     bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged); 
     bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted); 
     bgw.WorkerReportsProgress = true; 
     bgw.RunWorkerAsync(); 
    } 

    void bgw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     int total = 57; //some number (this is your variable to change)!! 

     for (int i = 0; i <= total; i++) //some number (total) 
     { 
      System.Threading.Thread.Sleep(100); 
      int percents = (i * 100)/total; 
      bgw.ReportProgress(percents, i); 
      //2 arguments: 
      //1. procenteges (from 0 t0 100) - i do a calcumation 
      //2. some current value! 
     } 
    } 

    void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     progressBar1.Value = e.ProgressPercentage; 
     label1.Text = String.Format("Progress: {0} %", e.ProgressPercentage); 
     label2.Text = String.Format("Total items transfered: {0}", e.UserState); 
    } 

    void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     //do the code when bgv completes its work 
    } 
} 

您可以設置進度條的可見爲false bgw_RunWorkerCompleted
下面的鏈接將展示如何使用backgroundworker
DotNetPerls
MSDN Reference
CodeProject
好運:)

+0

在代碼中有一個問題,當我點擊第一次進度條運行一次,如果我點擊第二次運行2次,等等..其他明智的代碼是完美的工作.. @zey – coolprarun

+0

你可以使用'BackgroundWorker'告訴我你的代碼嗎? – zey

+0

我只使用你的代碼..上面的代碼ü張貼.. @zey – coolprarun

0

這是我使用的BackgroundWorker的代碼..

public partial class Form1 : Form 
{ 

    BackgroundWorker bgw = new BackgroundWorker();  
    public Form1() 
    { 
     InitializeComponent(); 
     label3.Text = ""; 


     this.StartPosition = FormStartPosition.CenterScreen; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     Application.Exit(); 
    } 


    private void btn_generate_Click(object sender, EventArgs e) 
    { 
     progressBar1.Visible = true; 
     bgw.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); 
     bgw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); 
     bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); 
     bgw.WorkerReportsProgress = true; 
     bgw.RunWorkerAsync(); 
    } 

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     int total = 100; //some number (this is your variable to change)!! 

     for (int i = 0; i <= total; i++) //some number (total) 
     { 
      System.Threading.Thread.Sleep(10); 
      int percents = (i * 100)/100; 
      bgw.ReportProgress(percents, i); 

     } 
    } 

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     progressBar1.Value = e.ProgressPercentage; 
     label3.Text = String.Format("Progress: {0} %", e.ProgressPercentage); 
     if (e.ProgressPercentage == 100) 
     { 
      label3.Text = String.Format("Generated.. {0} %", e.ProgressPercentage); 

     } 
     // label2.Text = String.Format("Total items transfered: {0}", e.UserState); 
    } 

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     progressBar1.Visible = false; 
    } 



}` 

在此有在下面的代碼中,當我單擊第一次進度條運行一次的按鈕時,如果我單擊s,則會出現問題第二次運行2次,等等..其他明智的代碼是完美的工作..