這個範例程式,會顯示進度字串到textbox內,textbox會自動捲動到最下方,會顯示進度條(0~100),可讓使用者中斷程序。
VB部分的程式碼我是用網路上C#toVB轉換器轉的,所以不知道有無錯誤,請網友費心試試看囉~

程式元件畫面:
NET背景執行
 

//C#

//---------設計畫面時的設定---------
this.button1.Text = "開始";
this.button2.Text = "停止";
this.textBox1.Multiline = true; //多行
this.textBox1.ScrollBars = ScrollBars.Vertical; //顯示垂直捲軸
this.backgroundWorker1.WorkerReportsProgress = true; //回報進度
this.backgroundWorker1.WorkerSupportsCancellation = true; //允許中斷
this.timer1.Interval = 1000;
//--------------------------------

string msg; //存放回報訊息
DateTime TimerTick; //計時器時間

private void button1_Click(object sender, EventArgs e)
{
    this.TimerTick = DateTime.Parse("2000/1/1 00:00:00"); //初始時間點
    this.timer1.Start(); //啟動計時器
    this.progressBar1.Visible = true; //顯示進度條
    this.backgroundWorker1.RunWorkerAsync(); //呼叫背景程式
}

private void button2_Click(object sender, EventArgs e)
{
    this.backgroundWorker1.CancelAsync(); //中斷背景程式
}

private void todo(BackgroundWorker worker, DoWorkEventArgs e)
{
    for (int i = 1; i <= 100; i++ )
    {
        if (worker.CancellationPending) //如果被中斷...
        {
            e.Cancel = true;
            break;
        }
        System.Threading.Thread.Sleep(300); //延遲300毫秒
        this.msg = "第 " + i + " 圈 ... \r\n"; //設定訊息
        worker.ReportProgress(i); //回報進度
    }
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    if (backgroundWorker1.CancellationPending) //如果被中斷...
        e.Cancel = true;

    BackgroundWorker worker = (BackgroundWorker)sender;
    this.todo(worker, e); //欲背景執行的function
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.textBox1.Text += msg;
    this.textBox1.SelectionStart = this.textBox1.Text.Length;
    this.textBox1.ScrollToCaret();
    this.textBox1.Refresh();
    this.progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if ((e.Error != null))
        MessageBox.Show(e.Error.Message);
    else if (e.Cancelled)
        MessageBox.Show("使用者中斷程式");
    else
        MessageBox.Show("完成");

    this.progressBar1.Visible = false; //隱藏進度條
    this.timer1.Stop(); //停止計時器
}

private void timer1_Tick(object sender, EventArgs e)
{
    this.TimerTick = this.TimerTick.AddSeconds(1); //計時器 每秒+1
    this.label1.Text = this.TimerTick.ToString("HH:mm:ss"); //設定顯示格式
}

'VB

'---------設計畫面時的設定---------
Me.button1.Text = "開始"
Me.button2.Text = "停止"
Me.textBox1.Multiline = True '多行
Me.textBox1.ScrollBars = ScrollBars.Vertical '顯示垂直捲軸
Me.backgroundWorker1.WorkerReportsProgress = True '回報進度
Me.backgroundWorker1.WorkerSupportsCancellation = True '允許中斷
Me.timer1.Interval = 1000
'--------------------------------

Private msg As String '存放回報訊息
Private TimerTick As DateTime '計時器時間

Private Sub button1_Click(sender As Object, e As EventArgs)
    Me.TimerTick = DateTime.Parse("2000/1/1 00:00:00") '初始時間點
    Me.timer1.Start() '啟動計時器
    Me.progressBar1.Visible = True '顯示進度條
    Me.backgroundWorker1.RunWorkerAsync() '呼叫背景程式
End Sub

Private Sub button2_Click(sender As Object, e As EventArgs)
    Me.backgroundWorker1.CancelAsync() '中斷背景程式
End Sub

Private Sub todo(worker As BackgroundWorker, e As DoWorkEventArgs)
    For i As Integer = 1 To 100
        If worker.CancellationPending Then '如果被中斷...
            e.Cancel = True
            Exit For
        End If
        System.Threading.Thread.Sleep(300) '延遲300毫秒
        Me.msg = "第 " & i & " 圈 ... " & vbCr & vbLf '設定訊息
        worker.ReportProgress(i) '回報進度
    Next
End Sub

Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs)
    If backgroundWorker1.CancellationPending Then '如果被中斷...
        e.Cancel = True
    End If

    Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
    Me.todo(worker, e) '欲背景執行的function
End Sub

Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs)
    Me.textBox1.Text += msg
    Me.textBox1.SelectionStart = Me.textBox1.Text.Length
    Me.textBox1.ScrollToCaret()
    Me.textBox1.Refresh()
    Me.progressBar1.Value = e.ProgressPercentage
End Sub

Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
    If (e.Error IsNot Nothing) Then
        MessageBox.Show(e.Error.Message)
    ElseIf e.Cancelled Then
        MessageBox.Show("使用者中斷程式")
    Else
        MessageBox.Show("完成")
    End If

    Me.progressBar1.Visible = False '隱藏進度條
    Me.timer1.Stop() '停止計時器
End Sub

Private Sub timer1_Tick(sender As Object, e As EventArgs)
    Me.TimerTick = Me.TimerTick.AddSeconds(1) '計時器 每秒+1
    Me.label1.Text = Me.TimerTick.ToString("HH:mm:ss") '設定顯示格式
End Sub

arrow
arrow
    文章標籤
    c# vb
    全站熱搜

    伊 發表在 痞客邦 留言(6) 人氣()