2013-06-18 67 views
2

對不起我的英文不好在窗體加載進度條

我想在C#這將從數據庫中獲取一些數據來設計一個雙贏的形式,而它加載

我想用一個進度欄顯示讀取數據的進度

我想這個代碼(以及許多其他)

protected override void OnLoad(EventArgs e) 
{ 
    base.OnLoad(e); 
    this.Show(); 

    progressBar1.Value = 10; 
    int n; 

    n = 50;//number of records in DB , 
    double progress = 0; 
    double progstep = 25/n; 
    for (int i = 1; i <= n; i++) 
    { 
     //getting 
     if (progress <= 100) 
      progressBar1.Value = (int)progress; 
    } 

    progressBar1.Value = 35; 

    n = 100;//number of records in DB for another data reading from DB , 
    progress = 35; 
    progstep = 65/n; 
    for (int i = 1; i <= n; i++) 
    { 
     //getting data from DB 
     dataGridView1.Rows.Add(....);//Adding that data to a datagrid -- parametrs removed. 
     progress += progress; 
     if (progress <= 100) 
      progressBar1.Value = (int)progress; 
    } 

} 

但是,問題是,表格會等待,直到數據讀取進度完成nd我可以看到一個完整的進度條和所有加載的數據。

我該怎麼做才能解決這個問題?

感謝

+4

使用線程來使其成爲後臺進度。 –

+1

嘗試使用backroundWorker。 –

+0

@bash。d,因爲我將在表單上使用數據網格,所以我不知道如何用後臺工作人員或線程完成此操作。 – Arashdn

回答

5

由於這是的WinForms,我建議使用BackgroundWorker

基本例如:

bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork); 
bgWorker.ProgressChanged += new DoWorkEventHandler(bgWorker_ProgressChanged); 
bgWorker.RunWorkerAsync(//pass in object to process) 

那麼這將開球:

private void bgWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    //Do all of your work here 
    bgWorker.ReportProgress(); //percent done calculation 
} 

則進度更改的事件將觸發以更新安全的UI:

private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 
{ 
    progressBar.Value = e.ProgressPercentage; 
} 
2

這是簡單的樣機向您展示如何與後臺工作人員合作:

首先在OnLoad創建後臺工作,並附加2個事件吧:

var bw = new BackgroundWorker();    
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(WorkCompleted); 
bw.DoWork += new DoWorkEventHandler(DoWork); 
bw.RunWorkerAsync(data); //assume data is list of numbers 

private void WorkCompleted(object sender, RunWorkerCompletedEventArgs e) //after work completed remove event handlers and dispose 
{ 
    var bw = (BackgroundWorker)sender; 
    bw.RunWorkerCompleted -= WorkCompleted; 
    bw.DoWork -= DoWork; 
    bw.Dispose(); 
} 

private void DoWork(object sender, DoWorkEventArgs e) 
{ 
    var data = (List<int>)e.Argument; 

    foreach (var number in data) 
    { 
     if (progressBar1.InvokeRequired) 
     { 
      progressBar1.Invoke((MethodInvoker)delegate { this.ProcessNumber(number); });      
     } 
     else 
     { 
      ProcessNumber(number); 
     } 
    } 
} 

private void ProcessNumber(int i) 
{ 
    progressBar1.PerformStep(); 

    //do something with i 
} 
0

看看BackgroundWorker的控制。在窗體加載調用

backgroundWorker.RunWorkerAsync(); 

,並覆蓋事件的DoWork做骯髒的工作(從數據庫加載數據)和ProgressChanged來更新進度條。在活動體(可以說事件簽名會是這樣):

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { 
    var worker = (BackgroundWorker)sender; 
    // time consuming operation 
    worker.ReportProgress(10, null); 
    // ... another stuff 
} 


private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { 
    progressBar.Value = e.ProgressPercentage; 
} 
2

添加backgroundWorker1到表單中。

再經過InitializeComponent()

backgroundWorker1.WorkerReportsProgress = true; 
     // This event will be raised on the worker thread when the worker starts 
     backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); 
     // This event will be raised when we call ReportProgress 
     backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); 

添加YourForm_Shown事件

private void YourForm_Shown(object sender, EventArgs e) 
    { 
     backgroundWorker1.RunWorkerAsync(); 
    } 

添加在窗體的構造器和去年新增的backgroundWorker1

void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      // The progress percentage is a property of e 
      progressBar1.Value = e.ProgressPercentage; 
     } 

0123空隙
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     for (int i = 1; i <= n; i++) 
     { 
     //getting data from DB 
      dataGridView1.Rows.Add(....);//Adding that data to a datagrid -- parametrs removed. 
        backgroundWorker1.ReportProgress(i); 
      // Simulate long task     
     } 
    }