在一個WinForm窗體上有三個進度條(分別爲a,b,c)和一個Button按鈕,點擊按鈕,程序開始運行,當a走100%時,b走10%,c走1%。請問這個程序要怎麼寫?
首先你要對線程有一定的瞭解。進度條的顯示、後臺的進度計算是在兩個線程裏面分別來運行的。不然會出現不同步的問題。
然後是委託。用委託來進行兩個線程之間的通信。
至於三個進度條,簡單:直接在計算進度的線程中,算好的進度,直接給主線程a走100%時,b走10%,c走1%,百分知多少就是參數的問題。
如果你的重點是 “a走100%時,b走10%,c走1%”。你只要在得到百分比的算法上作文章了。
比如:a的工作量:循環100次,那麼b循環1000次,c循環10000次。那麼同等時間裏就是你要的效果。
明白吧。這個功能在vs2005 中,有個控件是做個的。一個是那個進度條控件,一個就是 back..的控件。圖標很好認有半個進度條。
給分吧
補充例子:
namespace Pro
{
delegate void setprogressBar(int prent, string rogressBar_name);
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//聲明委託對象
setprogressBar set = new setprogressBar(SetValue);
//進度條的計算、直接用循環代替後臺的操作。
for (int i = 0; i < 10000; i++)
{
//跨線程調用
this.Invoke(set, new object[] { i / 100, "a" });
this.Invoke(set, new object[] { i / 1000, "b" });
this.Invoke(set, new object[] { i / 10000, "c" });
}
}
/// <summary>
/// 測試開始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}
/// <summary>
/// 主線程中設置進度數值
/// </summary>
/// <param name="prent">百分比</param>
/// <param name="name">控件名稱</param>
private void SetValue(int prent, string name)
{
switch (name)
{
case "a":
this.progressBar1.Value = prent;
break;
case "b":
this.progressBar2.Value = prent;
break;
case "c":
this.progressBar3.Value = prent;
break;
}
}
}
}
public
class
WorkerBar
{
private
EventWaitHandle _event;
private
EventWaitHandle _parentEvent;
private
ProgressBar _bar;
public
WorkerBar(ProgressBar bar,EventWaitHandle eventHandle,EventWaitHandle parentEventHandle)
{
this
._bar
=
bar;
this
._event
=
eventHandle;
this
._parentEvent
=
parentEventHandle;
}
private
delegate
void
PerformStepDelegate(ProgressBar progressBar);
public
void
PerformStep(ProgressBar progressBar)
{
if
(
!
progressBar.InvokeRequired)
{
progressBar.PerformStep();
}
else
{
PerformStepDelegate performStep
=
new
PerformStepDelegate(
this
.PerformStep);
progressBar.Invoke(performStep,
new
object
[] { progressBar });
}
}
public
EventWaitHandle Event
{
get
{
return
this
._event;
}
}
public
EventWaitHandle ParentEvent
{
get
{
return
this
._parentEvent;
}
}
public
ProgressBar Bar
{
get
{
return
this
._bar;
}
}
}
public
partial
class
Form1 : Form
{
private
List
<
WorkerBar
>
_workerBars;
private
WaitHandle[] _waitEvents;
public
Form1()
{
InitializeComponent();
_workerBars
=
new
List
<
WorkerBar
>
();
_waitEvents
=
new
WaitHandle[
3
];
}
private
void
button1_Click(
object
sender, EventArgs e)
{
for
(
int
i
=
0
; i
<
3
; i
++
)
{
_waitEvents[i]
=
new
ManualResetEvent(
false
);
}
_workerBars.Add(
new
WorkerBar(
this
.progressBar1, (EventWaitHandle)_waitEvents[
0
], (EventWaitHandle)_waitEvents[
1
]));
_workerBars.Add(
new
WorkerBar(
this
.progressBar2, (EventWaitHandle)_waitEvents[
1
], (EventWaitHandle)_waitEvents[
2
]));
_workerBars.Add(
new
WorkerBar(
this
.progressBar3, (EventWaitHandle)_waitEvents[
2
], (EventWaitHandle)_waitEvents[
0
]));
Thread th1
=
new
Thread(ProgressBar1ThreadProc);
Thread th2
=
new
Thread(ProgressBar2ThreadProc);
Thread th3
=
new
Thread(ProgressBar3ThreadProc);
th1.Start(_workerBars[
0
]);
th2.Start(_workerBars[
1
]);
th3.Start(_workerBars[
2
]);
}
///
<summary>
///
工作線程 1
///
</summary>
private
static
void
ProgressBar1ThreadProc(
object
obj)
{
WorkerBar workerBar
=
(WorkerBar)obj;
while
(workerBar.Bar.Value
!=
workerBar.Bar.Maximum)
{
Thread.Sleep(
100
);
workerBar.PerformStep(workerBar.Bar);
workerBar.ParentEvent.Set();
//
通知工作線程 1
}
}
///
<summary>
///
工作線程 2
///
</summary>
private
static
void
ProgressBar2ThreadProc(
object
obj)
{
WorkerBar workerBar
=
(WorkerBar)obj;
while
(workerBar.Bar.Value
!=
workerBar.Bar.Maximum)
{
workerBar.Event.WaitOne();
//
等待工作線程 1 通知
workerBar.PerformStep(workerBar.Bar);
workerBar.Event.Reset();
workerBar.ParentEvent.Set();
//
通知工作線程 3
}
}
///
<summary>
///
工作線程 3
///
</summary>
private
static
void
ProgressBar3ThreadProc(
object
obj)
{
WorkerBar workerBar
=
(WorkerBar)obj;
while
(workerBar.Bar.Value
!=
workerBar.Bar.Maximum)
{
workerBar.Event.WaitOne();
//
等待工作線程 2 通知
workerBar.PerformStep(workerBar.Bar);
workerBar.Event.Reset();
}
}
}
partial
class
Form1
{
///
<summary>
///
必需的設計器變量。
///
</summary>
private
System.ComponentModel.IContainer components
=
null
;
///
<summary>
///
清理所有正在使用的資源。
///
</summary>
///
<param name="disposing">
如果應釋放託管資源,爲 true;否則爲 false。
</param>
protected
override
void
Dispose(
bool
disposing)
{
if
(disposing
&&
(components
!=
null
))
{
components.Dispose();
}
base
.Dispose(disposing);
}
#region
Windows 窗體設計器生成的代碼
///
<summary>
///
設計器支持所需的方法 - 不要
///
使用代碼編輯器修改此方法的內容。
///
</summary>
private
void
InitializeComponent()
{
this
.button1
=
new
System.Windows.Forms.Button();
this
.progressBar1
=
new
System.Windows.Forms.ProgressBar();
this
.progressBar2
=
new
System.Windows.Forms.ProgressBar();
this
.progressBar3
=
new
System.Windows.Forms.ProgressBar();
this
.SuspendLayout();
//
//
button1
//
this
.button1.Location
=
new
System.Drawing.Point(
578
,
42
);
this
.button1.Name
=
"
button1
"
;
this
.button1.Size
=
new
System.Drawing.Size(
75
,
23
);
this
.button1.TabIndex
=
0
;
this
.button1.Text
=
"
button1
"
;
this
.button1.UseVisualStyleBackColor
=
true
;
this
.button1.Click
+=
new
System.EventHandler(
this
.button1_Click);
//
//
progressBar1
//
this
.progressBar1.Location
=
new
System.Drawing.Point(
12
,
42
);
this
.progressBar1.Maximum
=
1000
;
this
.progressBar1.Name
=
"
progressBar1
"
;
this
.progressBar1.Size
=
new
System.Drawing.Size(
533
,
23
);
this
.progressBar1.Step
=
100
;
this
.progressBar1.TabIndex
=
1
;
//
//
progressBar2
//
this
.progressBar2.Location
=
new
System.Drawing.Point(
12
,
107
);
this
.progressBar2.Maximum
=
1000
;
this
.progressBar2.Name
=
"
progressBar2
"
;
this
.progressBar2.Size
=
new
System.Drawing.Size(
533
,
23
);
this
.progressBar2.TabIndex
=
1
;
//
//
progressBar3
//
this
.progressBar3.Location
=
new
System.Drawing.Point(
12
,
163
);
this
.progressBar3.Maximum
=
1000
;
this
.progressBar3.Name
=
"
progressBar3
"
;
this
.progressBar3.Size
=
new
System.Drawing.Size(
533
,
23
);
this
.progressBar3.Step
=
1
;
this
.progressBar3.TabIndex
=
1
;
//
//
Form1
//
this
.AutoScaleDimensions
=
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode
=
System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize
=
new
System.Drawing.Size(
689
,
366
);
this
.Controls.Add(
this
.progressBar3);
this
.Controls.Add(
this
.progressBar2);
this
.Controls.Add(
this
.progressBar1);
this
.Controls.Add(
this
.button1);
this
.Name
=
"
Form1
"
;
this
.Text
=
"
Form1
"
;
this
.ResumeLayout(
false
);
}
#endregion
private
System.Windows.Forms.Button button1;
private
System.Windows.Forms.ProgressBar progressBar1;
private
System.Windows.Forms.ProgressBar progressBar2;
private
System.Windows.Forms.ProgressBar progressBar3;
}