首页 新闻 搜索 专区 学院

如何统计发送成功的邮件数量和花费时间

1
悬赏园豆:20 [已解决问题] 解决于 2010-11-15 15:07

 

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();
}

 

 



Fry_CiCi的主页 Fry_CiCi | 初学一级 | 园豆:0
提问于:2010-11-12 14:17
< >
分享
最佳答案
0

抱歉,没发全,代码是这样

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;
}
}


}

 

收获园豆:20
wgz | 小虾三级 |园豆:1254 | 2010-11-12 21:15
不行 运行的时候有错误 !
Fry_CiCi | 园豆:0 (初学一级) | 2010-11-15 10:06
不可能吧,我自己这边试了下可以的,你完全拷贝过去运行出错?你的代码怎么样的,要不信息里发给我看看
wgz | 园豆:1254 (小虾三级) | 2010-11-15 15:27
现在可以啦 不过我把你的代码改的面目全非 呵呵 我的容易懂点 !! 主要是利用了AutoResetEvent 这个来做统计的 if (this.totalCount == 0) { //处理完成发送信号 autoEvent.Set(); } 我觉得你这样的判断并不是很合理 嘿嘿
Fry_CiCi | 园豆:0 (初学一级) | 2010-11-16 14:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册