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(xxxx@163.com, "c3xx", xxxx@qq.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();
}
抱歉,没发全,代码是这样
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
EmailMessage[] array = new EmailMessage[5000098];
for (int i = 0; i < 5000098; i++)
{
array[i] = new EmailMessage(i + "", i + "");
}
MessageSend send = new MessageSend(10, array);
//开始执行
DateTime t1 = DateTime.Now;
int successCount = send.Execute();
DateTime t2 = DateTime.Now;
Console.WriteLine("总共花费时间:" + t2.Subtract(t1).Seconds + "秒");
Console.WriteLine("成功发送:" + successCount);
Console.ReadLine();
}
}
public class MessageSend
{
private int totalCount, threadCount, successCount;
private int perExecute;
private bool[] resultArray;
private EmailMessage[] messageArray;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="threadCount">生成委托个数</param>
/// <param name="messageArray">处理信息总量</param>
public MessageSend(int threadCount, EmailMessage[] messageArray)
{
this.totalCount = messageArray.Length;
this.messageArray = messageArray;
this.resultArray=new bool[totalCount];
this.threadCount = threadCount;
this.perExecute = (int)(totalCount / threadCount);
}
delegate void MessageSendDelegate(int startIndex, int endIndex);
private AutoResetEvent autoEvent = new AutoResetEvent(false);
private object lockObject = new object();
private void Send(int startIndex, int endIndex)
{
EmailMessage message;
for (int i = startIndex - 1; i < endIndex; i++)
{
message = messageArray[i];
//测试
resultArray[i] = int.Parse(message.Title) % 2 == 0;
//实际 resultArray[i] = SendMessage(message.Title,message.Body)
}
lock (lockObject)
{
this.totalCount -= (endIndex - startIndex + 1);
}
}
private void SendCallBack(IAsyncResult result)
{
MessageSendDelegate ms = (MessageSendDelegate)result.AsyncState;
ms.EndInvoke(result);
if (this.totalCount == 0)
{
//处理完成发送信号
autoEvent.Set();
}
}
public int Execute()
{
int count=this.totalCount;
MessageSendDelegate ms;
for (int i = 1; i <= threadCount; i++)
{
ms = new MessageSendDelegate(this.Send);
ms.BeginInvoke(perExecute * (i - 1) + 1, perExecute * i, SendCallBack, ms);
}
int lastIndex = perExecute * threadCount;
if (lastIndex < count)
{
ms = new MessageSendDelegate(this.Send);
ms.BeginInvoke(lastIndex + 1, count, SendCallBack, ms);
}
//停止等待信号
autoEvent.WaitOne();
foreach (bool result in resultArray)
{
if (result)
{
this.successCount++;
}
}
return successCount;
}
}
public class EmailMessage
{
public string Title { get; set; }
public string Body { get; set; }
public EmailMessage(string title, string body)
{
this.Title = title;
this.Body = body;
}
}
}