Safe, Simple Multithreading in Windows Forms, Part 3
Safe, Simple Multithreading in Windows Forms, Part 3
! "
& &7
! % 08
#
$ # % & 5 # &
# % # % ,- # 9 # &
. # * # # % !
// & "
# 0 +/()(++) 1 0
- 0 . 2 2 . & # 0 #
#% 0 & *- 0 0 3 0 * # # -3 2 . & # 2 # 0 - .
% - . . % # - 4 # - # *-0 % # - #
# 0 ! % # &* # # % ,- #
. # * # # % !
5 0 0 * % # & ! *6 ( # # % 0 % &
% 0 & . # * &% # # 0 & 0% % % 0 # . #
# ,- # 7
5 # % & % % .0 # ,- # * & 0 # # # %
# # $ % # % % 0 0 * #
% % & 1 # ,- # & # 0 # .
# # 0 # 1 * # % & 1 0 & % & $ &
# # * % % % . # 0 &7
1 de 6 02-12-2005 18:36
Safe, Simple Multithreading in Windows Forms, Part 3 (Wonders of Wi... http://msdn.microsoft.com/library/en-us/dnforms/html/winforms0123200...
! % * # . * # % . 4% 0 # . # 0 #
0 * # %&# % % % 0 " * 3 #
# # % % & :% . # . 0 % % * %
# . 0 & # # # & "* # # 3 #
& % & # ; . <" - 3 % # 0 % # 0
# 0 # & # -0 # 1 & # & :% # - . % %
00* - 0 & # & % # 0 # ! .7
public ShowProgressArgs(
string pi, int totalDigits, int digitsSoFar) {
this.Pi = pi;
this.TotalDigits = totalDigits;
this.DigitsSoFar = digitsSoFar;
}
}
void ShowProgress(...) {
// Make sure we're on the right thread
if( this.InvokeRequired == false ) {...}
}
// Transfer control to correct thread
else {
ShowProgressHandler
showProgress =
new ShowProgressHandler(AsynchCalcPiForm_ShowProgress);
object sender = System.Threading.Thread.CurrentThread;
ShowProgressArgs
e = new ShowProgressArgs(pi, totalDigits, digitsSoFar);
Invoke(showProgress, new object[] {sender, e});
cancel = e.Cancel;
}
}
# 3 # & & % * # * % # . #
0 # " 4 # &% # ,- # # %&#
# # . . # # * % & #$ " 9 4 - % %
# # & * . & % # &% 0 # " 4 0 % $ &
% # . # * % % # 0 & # % % 1 & % # "
0 * # %&# 0 # . # 0 # ,-
# * # 3 & 0 % # . # 3 # ,- # . & # #
0 & &
# % & % 0% # 0 0 # 0% 0 " 0*
& # 0 # 7
2 de 6 02-12-2005 18:36
Safe, Simple Multithreading in Windows Forms, Part 3 (Wonders of Wi... http://msdn.microsoft.com/library/en-us/dnforms/html/winforms0123200...
}
}
// Transfer control to correct thread
else {
ShowProgressHandler
showProgress =
new ShowProgressHandler(ShowProgress);
Invoke(showProgress, new object[] { sender, e});
}
}
# # :% # * 0 * & 9 % -3 % 3
# # . & & * & # ,-* # # . & # & 0 1 0
% & # ,- # 3 & 0 # & -3 0 % # # 0 # & # * &
# # . # * & # ,- # * # % & # . # # % %
# # -3 & 9 % % 0 # . ,- # %
0 0 &* # 0 # " & & #
% # % % & % ,- . & #
" ' $
= 0 # # 3 % ! # %
. & & # 1 # # & # % &
* % # 6 > - & ! .9 # % & 0 % &
9 *0 % & 7
return pi.ToString();
}
}
3 de 6 02-12-2005 18:36
Safe, Simple Multithreading in Windows Forms, Part 3 (Wonders of Wi... http://msdn.microsoft.com/library/en-us/dnforms/html/winforms0123200...
:% 0 .0 # & 0 3 0 * 3 % # 9 1
& % ? % % @ # ( % # # " ') # % " '
$ & % # ,5 0 # &% & " # #
3 . A# 7BB # B 6 B 6 1< 0 1 - #
% 4 0 # 7BB # B 6 B 6 1< * 3 . #
% 1 % # C % BC6 4 B
5 0 BC B5 0 B5 0 # & 1 0 # . .
# 7
namespace AsynchCalcPi.localhost {
[WebServiceBindingAttribute(Name="CalcPiServiceSoap", ...)]
public class CalcPiService : SoapHttpClientProtocol {
public CalcPiService() {
this.Url =
"http://localhost/CalcPiWebService/CalcPiService.asmx";
}
[SoapDocumentMethodAttribute("http://tempuri.org/CalcPi", ...)]
public string CalcPi(int digits) {...}
public IAsyncResult
BeginCalcPi(
int digits,
System.AsyncCallback callback,
object asyncState) {...}
enum CalcState {
Pending,
Calculating,
Canceled,
}
case CalcState.Calculating:
state = CalcState.Canceled;
calcButton.Enabled = false;
service.Abort(); // Fail all outstanding requests
break;
case CalcState.Canceled:
Debug.Assert(false);
break;
}
}
4 de 6 02-12-2005 18:36
Safe, Simple Multithreading in Windows Forms, Part 3 (Wonders of Wi... http://msdn.microsoft.com/library/en-us/dnforms/html/winforms0123200...
# * 0 & % & # # % * * . * #
# * 0 " ' & & * # # % . %" ) #
&% # # # # # # & % # #
# # % 0 # & %
# # # * # # # # % # &7
+ , ' $
# # # # # . 0 . :% . * % $ &
0 0 # % # # ,- # # % # 0
# # # # ,- # * . # # . #
,- # 0 % # & 0 #
" -
'% # % # %&# 0 & *-# # & % # # % #
! 0 # 1 & % # # ,- 0
&9 % & * % # ,- # % 0% # % % . # . # 4%
# # % # % # * # % & 9 & # #
5 de 6 02-12-2005 18:36
Safe, Simple Multithreading in Windows Forms, Part 3 (Wonders of Wi... http://msdn.microsoft.com/library/en-us/dnforms/html/winforms0123200...
# 0 # ; ! * # 0% # 1 . & 0 # %
* # # # # :% . # . & % 0
#
. # 1 0 # 0 # & 9 7
# +)(///D(+E" 6 # # 5 ! 0 #
# % # .
% * . %# ; & % = 3
. % . & ! "
#! - # 0 * # # % 0 * # F &# % 9 4 * #5
* & * . 0# 0 0 & 0 % # * # % 4 *
# 7BB #
6 9
& &7
/ 0 -
! % 08
/ ( ) G H D I E 8
6 =% &
/ ( ) G H D I E 8
, 1 2
+3 # # &
6 de 6 02-12-2005 18:36