如何统计数量和花费时间!
delegate bool sendMsgDelegate(string name, string msg); //申明委托
private static object obj = new object();
static int count = 0; //计数
public static bool sendMsg(string name, string msg)
{
return EmailSend.SendEmail("xxx@xxx.com", "xxx", "xxx@xx.com", name, msg, "smtp.163.com");
// Console.Write(name);
}
public static void SendCallBack(IAsyncResult asynceresult)
{
sendMsgDelegate ss = (sendMsgDelegate)asynceresult.AsyncState;
lock (obj)
{
bool result = ss.EndInvoke(asynceresult);
if (result)
{
count++;
}
}
}
static void Main(string[] args)
{
DateTime t1 = DateTime.Now;
sendMsgDelegate ss = new sendMsgDelegate(sendMsg);
for (int i = 0; i < 10; i++)
{
IAsyncResult asyceresult = ss.BeginInvoke(" " + i, "Hello Worlds" + i, SendCallBack, ss);
}
DateTime t2 = DateTime.Now;
Console.WriteLine();
Console.WriteLine("总共花费时间:" + (t2 - t1));
Console.WriteLine("成功发送" + count);
Console.ReadLine();
}