using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading;
namespace
ConsoleApplication1
{
class
Program
{
static
void
Main(
string
[] args)
{
Console.WriteLine(
"Main ThreadId = "
+ Thread.CurrentThread.ManagedThreadId);
Func<
long
,
long
> delegateMethod =
new
Func<
long
,
long
>(CalcSum);
delegateMethod.BeginInvoke(200, DoneCallback, delegateMethod);
delegateMethod.BeginInvoke(10000000000, DoneCallback, delegateMethod);
Console.ReadLine();
}
static
void
DoneCallback(IAsyncResult asyncResult)
{
Console.WriteLine(
"DoneCallback ThreadId = "
+ Thread.CurrentThread.ManagedThreadId);
Func<
long
,
long
> method = (Func<
long
,
long
>)asyncResult.AsyncState;
try
{
long
sum = method.EndInvoke(asyncResult);
Console.WriteLine(
"sum = {0}"
,sum);
}
catch
(OverflowException)
{
Console.WriteLine(
"运算溢出了"
);
}
}
static
long
CalcSum(
long
topLimit)
{
Console.WriteLine(
"Calc ThreadId = "
+ Thread.CurrentThread.ManagedThreadId);
checked
{
long
result = 0;
for
(
long
i = 0; i < topLimit; i++)
{
result += i;
}
return
result;
}
}
}
}