我建了一个exe扫描程序 用于邮件群发 有邮件服务器的白名单
窗体上有一个button和一个timer
我点击button的时候
button1.Enabled = false;
timer1.Interval = 1000; //1秒执行一次
timer1.Start();
在 private void timer1_Tick(object sender, System.EventArgs e)
{
//在这里使用多线程
threads = new Thread[9];
for (int i = 0; i < 9; i++)
{
//将每一个线程都指向printer的PrintNumbers()方法
threads[i] = new Thread(new ThreadStart(MailSend));
//给每一个线程编号
threads[i].Name = i.ToString() + "号线程";
}
foreach (Thread t in threads)
{
t.Start();
System.Threading.Thread.Sleep(50);
}}
[MethodImpl(MethodImplOptions.Synchronized)]
private static void MailSend()
{
//执行发送代码块
}现在的问题是:每执行一次就会新增加10个线程 这样时间长了服务器会吃不消 而且进程会挂掉。请问各位大位有做过类似的吗?我现在的要求是每秒发送10条邮件。怎么样在定时器启动的时候而不去增加线程呢?或者线程执行完释放掉
窗体上有一个button和一个timer
我点击button的时候
button1.Enabled = false;
timer1.Interval = 1000; //1秒执行一次
timer1.Start();
在 private void timer1_Tick(object sender, System.EventArgs e)
{
//在这里使用多线程
threads = new Thread[9];
for (int i = 0; i < 9; i++)
{
//将每一个线程都指向printer的PrintNumbers()方法
threads[i] = new Thread(new ThreadStart(MailSend));
//给每一个线程编号
threads[i].Name = i.ToString() + "号线程";
}
foreach (Thread t in threads)
{
t.Start();
System.Threading.Thread.Sleep(50);
}}
[MethodImpl(MethodImplOptions.Synchronized)]
private static void MailSend()
{
//执行发送代码块
}现在的问题是:每执行一次就会新增加10个线程 这样时间长了服务器会吃不消 而且进程会挂掉。请问各位大位有做过类似的吗?我现在的要求是每秒发送10条邮件。怎么样在定时器启动的时候而不去增加线程呢?或者线程执行完释放掉
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货