: 謝謝你 那關於worker thread是什麼我大概知道了,
: 有人要解答control.BeginInvoke跟 delegate.BeginInvoke的不同嗎= =?
: 這位大大講的這個範例我有看過,觀念我也懂得
: 其實這提的疑問也是來自於這篇文章
: http://msdn2.microsoft.com/en-us/library/ms951089.aspx
: 他在裡面同時用到Control.BeginInvoke跟delegate.BeginInvoke
: 搞得我....探口氣
貼上我的問題點
Button_click()算是程式開始點 抓到totalDigits:要算到小數點後幾位
並依照目前進度 到progressbar去顯示進度
static void Main() {
Application.Run(new Form1());
}
delegate void ShowProgressDelegate(string pi, int totalDigits, int
digitsSoFar);
void ShowProgress(string pi, int totalDigits, int digitsSoFar) {
// Make sure we're on the right thread
if( _pi.InvokeRequired == false ) {
_pi.Text = pi;
_piProgress.Maximum = totalDigits;
_piProgress.Value = digitsSoFar;
}
else {
// Show progress asynchronously
ShowProgressDelegate showProgress = new
ShowProgressDelegate(ShowProgress);
//Invoke(showProgress, new object[] { pi, totalDigits, digitsSoFar});
BeginInvoke(showProgress, new object[] { pi, totalDigits,
digitsSoFar});
}使用control.BeginInvoke
}
void CalcPi(int digits) {
StringBuilder pi = new StringBuilder("3", digits + 2);
// Show progress
ShowProgress(pi.ToString(), digits, 0);
if( digits > 0 ) {
pi.Append(".");
for( int i = 0; i < digits; i += 9 ) {
int nineDigits = NineDigitsOfPi.StartingAt(i+1);
int digitCount = Math.Min(digits - i, 9);
string ds = string.Format("{0:D9}", nineDigits);
pi.Append(ds.Substring(0, digitCount));
// Show progress
ShowProgress(pi.ToString(), digits, i + digitCount);
}
}
}
delegate void CalcPiDelegate(int digits);
private void _calcButton_Click(object sender, System.EventArgs e) {
// Synch method
// CalcPi((int)_digits.Value);
// Asynch delegate method
CalcPiDelegate calcPi = new CalcPiDelegate(CalcPi);
calcPi.BeginInvoke((int)_digits.Value, null, null);
}使用delegate.BeginInvoke
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.25.101.28
※ 編輯: reptile0426 來自: 163.25.101.28 (11/21 17:37)
※ 編輯: reptile0426 來自: 163.25.101.28 (11/21 17:44)
※ 編輯: reptile0426 來自: 163.25.101.28 (11/21 21:03)