delegate bool sendMsgDelegate(string name, string msg); //申明委托
public static bool sendMsg(string name, string msg)
{
return EmailSend.SendEmail("[email protected]", "xxxxxx", "[email protected]", name, msg, "smtp.163.com");
} public static void SendCallBack(IAsyncResult asynceresult)
{
sendMsgDelegate ss = (sendMsgDelegate)asynceresult.AsyncState;
bool result = ss.EndInvoke(asynceresult);
}
static void Main(string[] args)
{
sendMsgDelegate ss = new sendMsgDelegate(sendMsg);
for (int i = 0; i < 5; i++)
{
IAsyncResult asyceresult = ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, ss);
}
Console.ReadLine();
}
解决方案 »
- 如何动态换ip,ip被baidu,google禁止访问。
- 问个技术问题
- 大家来谈谈:"在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件"这个错误得原因!
- 关于这个js提示框的问题!
- Page.PreviousPage.FindControl("ctl00$ContentPlaceHolder1$lblinvino");是什么意思(急!在线等)
- 新手提问:关于存储过程、和视图
- DataGrid数据列显示 / 隐藏问题!!!
- 一个页面执行完后,如何同时刷新别一个页面
- 输出日期,如果是今天,就显示红色,不然就显示黑色
- ASP.NET中页面刷新问题
- 复制dll到web 的bin文件夹下就出错
- 急~~~怎么把TXT文件循环导入SQL数据库
{
IAsyncResult asyceresult = ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, ss);
}
这个I是指的开的线程数量吗???
public static void SendCallBack(IAsyncResult asynceresult)
{
sendMsgDelegate ss = (sendMsgDelegate)asynceresult.AsyncState;
bool result = ss.EndInvoke(asynceresult);
}
进行处理???
result 就是一个线程调用sendMsg这个函数的返回值
for (int i = 0; i < 5; i++)
{
IAsyncResult asyceresult = ss.BeginInvoke("jack" + i, "Hello Worlds" + i, SendCallBack, ss);
}
for循环是不是等这个方法里面的那个方法结束之后进入下一轮循环
就是我想统计下有多少邮件是发送成功 有多少是失败的 好统计吗??
在那个callback里面怎么写呢?
开了5个线程,也就是说发了5次,每个线程执行完发送邮件以后都会有一个result
定义一个静态计数器,每个result为true的时候,这个静态变量+1试试看
public bool sendMsg(string name, string msg)
{
lock (this)
{ Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
return SendEmail("[email protected]", "xxxxxx", "[email protected]", name, msg, "smtp.163.com");
}
} private static bool SendEmail(string p, string p_2, string p_3, string name, string msg, string p_6)
{
return true;
} public static void SendCallBack(IAsyncResult asynceresult)
{
sendMsgDelegate ss = (sendMsgDelegate) ((AsyncResult)asynceresult).AsyncDelegate;
bool result = ss.EndInvoke(asynceresult);
Console.WriteLine(result);
} [Test]
public void Test()
{
sendMsgDelegate ss = sendMsg;
for (int i = 0; i < 5; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
} //Console.ReadLine();
}发现不一定是5个线程的ID,有时是两个,有时是3个
for (int i = 0; i < 5; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
Console.WriteLine("我是主方法里的");
}
多运行几次,每次结果都不一样的。
如果我要进行邮件的批量发送的话 , public void Test()
{
sendMsgDelegate ss = sendMsg;
for (int i = 0; i < 5; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
}}
如果 我要发送 100000个邮箱 难道是
for (int i = 0; i < 1000000; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
}这样写吗??
{
public Form1()
{
InitializeComponent();
doSomeThred();
} /// <summary>
/// 异步线程
/// </summary>
private void doSomeThred()
{
doThreadSynch();
} private void DoSomeThing(int i, int j)
{
try
{
RichTextBox ri = this.Controls["richTextBox" + (i + 1)] as RichTextBox;
ri.Text += "dobegin\r\n";
Thread.Sleep(2000);
ri.Text += "doend\r\n";
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
} private void CallBackMethod(IAsyncResult ar)
{
doDelegate d = (doDelegate)ar.AsyncState;
//this.richTextBox1.Text += "Threadend\r\n";
this.richTextBox6.Text = this.richTextBox6.Text + "执行成功一次";
this.richTextBox7.Text = this.richTextBox7.Text + Thread.CurrentThread.Name;
d.EndInvoke(ar);
} private delegate void doDelegate(int i, int j); private void doThreadSynch()
{
doDelegate dlg = new doDelegate(DoSomeThing);
AsyncCallback acb = new AsyncCallback(CallBackMethod);
//this.richTextBox1.Text += "ThreadBegin\r\n";
for (int i = 0; i < 5; i++)
{
IAsyncResult iar = dlg.BeginInvoke(i, i, acb, dlg);
}
} }
我测试了一下
for循环不是等这个方法里面的那个方法结束之后进入下一轮循环,是直接开始一个线程,
另外楼上说的线程不是开了5个..请问怎么获取到线程的Id呢?
我用了Thread.CurrentThread.Name 好像没有东西,所以我不知道怎么测试,谢谢了
另外要统计成功发送邮件,需要lock...否则结果不准我的代码需要改一下要这样
private void CallBackMethod(IAsyncResult ar)
{
doDelegate d = (doDelegate)ar.AsyncState;
//this.richTextBox1.Text += "Threadend\r\n"; lock (obj)
{
this.richTextBox6.Text = this.richTextBox6.Text + "执行成功一次";
} d.EndInvoke(ar);
} private static object obj = new object();
有时候5个,有时候4个,有时候3个
{
sendMsgDelegate ss = sendMsg;
for (int i = 0; i < 5; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
}}
如果 我要发送 100000个邮箱 难道是
for (int i = 0; i < 1000000; i++)
{
ss.BeginInvoke("jack" + i, "Hello" + i, SendCallBack, null);
}这样写吗??
线程的个数是不是在I的范围之类 或是跟I有关啊?
这个是受线程池管理的..估计有线程重用的概念...比如说我返回的线程id是4 3 5 6 4估计4被重复使用了。。这个不太清楚呢,要研究一下线程池的机制,不过可以肯定的是线程的数量不会大于I
{
for (int i = 0; i < 10000000; i++)
{
sendMsg();
}
});
DateTime t1 = DateTime.Now;
th1.Start();
while (true)
{
if (th1.ThreadState != System.Threading.ThreadState.Stopped)
{
continue;
}
else
{
break;
}
}
th1.Abort(); DateTime t2 = DateTime.Now;
Console.WriteLine("总共花费时间:" + (t2 - t1)); Console.ReadLine(); 我这样测试了一下发现用的时间还不到一秒
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();
我发现这样写的无法进行统计呢!!!
{
sendMsgDelegate ss = (sendMsgDelegate)asynceresult.AsyncState;
lock (obj)
{
bool result = ss.EndInvoke(asynceresult);
if (result)
{
Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
count++;
}
}
}
private static object obj = new object();
static int count = 0;