如何让程序暂停几秒再执行 for (int j = 0; j < alist.Count; j++) { Thread mythread = new Thread(new ThreadStart(mu.add)); mythread.Start(); }如何实现每循环一次暂停几秒再继续执行呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 System.Threading.Thread.Sleep(5000);//进程休眠5秒! 加在哪里呢?mythread.Start();下面吗? public void SendMail2() { All.runing++; try { add1(); All.success++; All.runing--; } catch (Exception ex) { All.fail++; All.runing--; } }具体加在哪里呢? 参考一下代码,控制台程式 static Thread th = null; static void Main(string[] args) { SleepThread(); } static void SleepThread() { for (int i = 0; i <= 2; i++) { th = new Thread(ShowTime); th.Start(); Thread.Sleep(1000); Console.WriteLine(i.ToString()); } } static void ShowTime() { Console.WriteLine(DateTime.Now.ToString()); } Thread.Sleep(int i) //i的单位是1/1000秒 Thread.Sleep()方法,可以的,如果控件台程序,也可以用TIMER控件, 在你 mm.add()方法 中加入 Thread.Sleep(i)(i为要暂停的毫秒数) Thread mythread = new Thread(new ThreadStart(mu.add)); mythread.Start(); Thread.Sleep(5000); sleep 决对没有错的吧!! sleep就可以了。也可以用timer,事件里什么都不做。。 for (int j = 0; j < 10; j++) { System.Timers.Timer timer = new System.Timers.Timer(((double)j + 1) * 5 * 1000);//5秒 timer.Elapsed += new System.Timers.ElapsedEventHandler(time); timer.Enabled = true; timer.Start(); } private static void time(object source, System.Timers.ElapsedEventArgs e) { ((System.Timers.Timer)source).Close(); mu_add(); } private static void mu_add() { //... } 也可以这样: count = 10; System.Timers.Timer timer = new System.Timers.Timer(5 * 1000);//5秒 timer.Elapsed += new System.Timers.ElapsedEventHandler(time); timer.Enabled = true; timer.Start(); private static int count = 0; private static void time(object source, System.Timers.ElapsedEventArgs e) { if (count-- == 0) ((System.Timers.Timer)source).Close(); mu_add(); } private static void mu_add() { //... } System.Threading.Thread.Sleep(要暂停的毫秒数); Thread mythread =Thread.CurrentThread;mythread.sleep(3000); 用sleep就可以了啊 怎么会不可以呢 是 windows from 的话 加个 timer! 在timer_Click事件中运行 你要循环的代码,设置好时间等 嗯,是winform程序,有个button clike 事件触发那段程序。 看来大家都推荐sleep ,但是我加上之后会出现程序未响应的情况,这是什么原因呢? 用一个timer控件,把代码都放在Timer1_Tick事件里。protected void Timer1_Tick(object sender, EventArgs e){ //这里是你要执行的代码段 System.Threading.Thread.Sleep(要暂停的毫秒数); } 我再把该部分完整代码贴出来,希望大家在此基础上改:下面是主窗体程序部分: /*群发*/ private void button1_Click(object sender, EventArgs e) { if (!check()) return; //初始化 toolStripProgressBar1.Visible = true; toolStripStatusLabel3.Text = ""; toolStripProgressBar1.Maximum = t_to.Items.Count; ArrayList alist = new ArrayList(); for (int i = 0; i < t_to.Items.Count; i++) { string mail; if (i < senderLst.Items.Count) { mail = t_to.Items[i].ToString() + "," + senderLst.Items[i].ToString(); } else { mail = t_to.Items[i].ToString() + "," + senderLst.Items[i % senderLst.Items.Count].ToString(); } alist.Add(mail); } for (int j = 0; j < alist.Count; j++) { string senderStr = alist[j].ToString(); string[] str = senderStr.Split(new char[] { ',', ',',',' }); string stmpStr = str[1]; string fromStr = str[2]; string pwdStr = str[3]; string toMail = str[0]; mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths); Thread mythread = new Thread(new ThreadStart(mu.SendMail2)); mythread.Start(); } while (All.runing != 0) { toolStripProgressBar1.Value = t_to.Items.Count - All.runing; Application.DoEvents(); toolStripStatusLabel3.Text = "发送成功:" + All.success.ToString() + "条 发送失败:" + All.fail.ToString() + "条"; } toolStripProgressBar1.Visible = false; toolStripStatusLabel3.Text = "【结果】发送成功:"+All.success.ToString()+"条 发送失败:"+All.fail.ToString()+"条"; All.success = 0; All.fail = 0; MessageBox.Show("【结果】发送成功:" + All.success.ToString() + "条 发送失败:" + All.fail.ToString() + "条"); }下面是mailunit类中SendMail2方法代码: /*发邮件:线程中使用*/ public void SendMail2() { All.runing++; //创建smtpclient对象 System.Net.Mail.SmtpClient client = new SmtpClient(); client.Host = smtp; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(from, pwd); client.DeliveryMethod = SmtpDeliveryMethod.Network; //创建mailMessage对象 System.Net.Mail.MailMessage message = new MailMessage(from, to); message.Subject = subject; //正文默认格式为html message.Body = body; message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8; //添加附件 if (paths.Count != 0) { foreach (string path in paths) { Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet); message.Attachments.Add(data); } } MessageBox.Show("1"); System.Threading.Thread.Sleep(5000);// MessageBox.Show("2"); try { client.Send(message); All.success++; All.runing--; } catch (Exception ex) { All.fail++; All.runing--; } } for (int j = 0; j < alist.Count; j++) { string senderStr = alist[j].ToString(); string[] str = senderStr.Split(new char[] { ',', ',',',' }); string stmpStr = str[1]; string fromStr = str[2]; string pwdStr = str[3]; string toMail = str[0]; mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths); Thread mythread = new Thread(new ThreadStart(mu.SendMail2)); mythread.Start(); }可以改为:private ArrayList _alist;private void time(object source, System.Timers.ElapsedEventArgs e) { string senderStr = _alist[0].ToString(); _alist.RemoveAt(0); if(_alist.Count == 0) ((System.Timers.Timer)source).Close(); string[] str = senderStr.Split(new char[] { ',', ',',',' }); string stmpStr = str[1]; string fromStr = str[2]; string pwdStr = str[3]; string toMail = str[0]; mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths); mu.SendMail2() }_alist = alist;System.Timers.Timer timer = new System.Timers.Timer(5 * 1000);//5秒timer.Elapsed += new System.Timers.ElapsedEventHandler(time);timer.Enabled = true;timer.Start(); //用timer控件也可以 //全局变量 int i = 0; //timer的click事件里: for (int j = i; j < alist.Count; j++) { Thread mythread = new Thread(new ThreadStart(mu.add)); mythread.Start(); i++; break; } for (int j = 0; j < alist.Count; j++) { Thread mythread = new Thread(new ThreadStart(mu.add)); mythread.Start(); Thread.Sleep(1000); } 还是多线程,新的线程由Timer触发,只是这个new和Start由Timer代理了. 你这里的Sleep是休眠的主线程...不停的new 新线程。。有响应就诡异了 for (int j = 0; j < alist.Count; j++) { string senderStr = alist[j].ToString(); string[] str = senderStr.Split(new char[] { ',', ',',',' }); string stmpStr = str[1]; string fromStr = str[2]; string pwdStr = str[3]; string toMail = str[0]; mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths); Thread mythread = new Thread(new ThreadStart(mu.SendMail2)); mythread.Start(); if(j>0)mythread.Sleep(5000); }这样既简单有快捷的解决了你点击没反映的情况 自己试验下吧因为当你掉用则个函数的时候 他就会触发睡眠5秒,不是点了没反应 是要等待5秒就这样加了以后第一次等待去掉 就好了 肯定是放到一个button_click里面了。一点,一sleep,窗口就停止响应了。用appliction.DoEvent()配合一下吧。 多谢大家,问题已经解决,其实sleep是可以的,只不过窗体假死而已,为了避免窗体假死的情况,可以使用Application.DoEvents()。再次感谢大家的帮助! C# 如何实现EXCEL数据分列 急急急!!! DataGrid新问题 listView1相对复杂的问题,高手请进 急!急!急!急!急!关于在C/S构架下对SQL数据库的操作! 关于using jmail 的一点问题 补全代码:如何将产品代号这一列赋值给下拉框 请问:如何在DataGrid中显示与本身绑定DataSet相关联的另一个DataSet中的值 如何动态加载一个窗体 我怎么想也不明后,帮我看看这是为什么 求问实现可能性 AS3中的这句用C#怎么写? C#开发视频及语音通话
public void SendMail2()
{
All.runing++;
try
{
add1();
All.success++;
All.runing--; }
catch (Exception ex)
{
All.fail++;
All.runing--;
} }具体加在哪里呢?
static void Main(string[] args)
{
SleepThread();
} static void SleepThread()
{
for (int i = 0; i <= 2; i++)
{
th = new Thread(ShowTime);
th.Start();
Thread.Sleep(1000);
Console.WriteLine(i.ToString());
}
} static void ShowTime()
{
Console.WriteLine(DateTime.Now.ToString());
}
加入 Thread.Sleep(i)(i为要暂停的毫秒数)
mythread.Start();
Thread.Sleep(5000);
也可以用timer,事件里什么都不做。。
{
System.Timers.Timer timer = new System.Timers.Timer(((double)j + 1) * 5 * 1000);//5秒
timer.Elapsed += new System.Timers.ElapsedEventHandler(time);
timer.Enabled = true;
timer.Start();
} private static void time(object source, System.Timers.ElapsedEventArgs e)
{
((System.Timers.Timer)source).Close();
mu_add();
}
private static void mu_add()
{
//...
}
count = 10;
System.Timers.Timer timer = new System.Timers.Timer(5 * 1000);//5秒
timer.Elapsed += new System.Timers.ElapsedEventHandler(time);
timer.Enabled = true;
timer.Start(); private static int count = 0;
private static void time(object source, System.Timers.ElapsedEventArgs e)
{
if (count-- == 0) ((System.Timers.Timer)source).Close();
mu_add();
}
private static void mu_add()
{
//...
}
mythread.sleep(3000);
在timer_Click事件中运行 你要循环的代码,设置好时间等
protected void Timer1_Tick(object sender, EventArgs e)
{
//这里是你要执行的代码段 System.Threading.Thread.Sleep(要暂停的毫秒数); }
private void button1_Click(object sender, EventArgs e)
{
if (!check())
return;
//初始化
toolStripProgressBar1.Visible = true;
toolStripStatusLabel3.Text = "";
toolStripProgressBar1.Maximum = t_to.Items.Count; ArrayList alist = new ArrayList(); for (int i = 0; i < t_to.Items.Count; i++)
{
string mail;
if (i < senderLst.Items.Count)
{
mail = t_to.Items[i].ToString() + "," + senderLst.Items[i].ToString();
}
else
{
mail = t_to.Items[i].ToString() + "," + senderLst.Items[i % senderLst.Items.Count].ToString();
}
alist.Add(mail); } for (int j = 0; j < alist.Count; j++)
{
string senderStr = alist[j].ToString();
string[] str = senderStr.Split(new char[] { ',', ',',',' });
string stmpStr = str[1];
string fromStr = str[2];
string pwdStr = str[3];
string toMail = str[0];
mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths);
Thread mythread = new Thread(new ThreadStart(mu.SendMail2));
mythread.Start(); } while (All.runing != 0)
{
toolStripProgressBar1.Value = t_to.Items.Count - All.runing;
Application.DoEvents();
toolStripStatusLabel3.Text = "发送成功:" + All.success.ToString() + "条 发送失败:" + All.fail.ToString() + "条";
}
toolStripProgressBar1.Visible = false;
toolStripStatusLabel3.Text = "【结果】发送成功:"+All.success.ToString()+"条 发送失败:"+All.fail.ToString()+"条";
All.success = 0;
All.fail = 0;
MessageBox.Show("【结果】发送成功:" + All.success.ToString() + "条 发送失败:" + All.fail.ToString() + "条");
}
下面是mailunit类中SendMail2方法代码: /*发邮件:线程中使用*/
public void SendMail2()
{
All.runing++;
//创建smtpclient对象
System.Net.Mail.SmtpClient client = new SmtpClient();
client.Host = smtp;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(from, pwd);
client.DeliveryMethod = SmtpDeliveryMethod.Network; //创建mailMessage对象
System.Net.Mail.MailMessage message = new MailMessage(from, to);
message.Subject = subject;
//正文默认格式为html
message.Body = body;
message.IsBodyHtml = true;
message.BodyEncoding = System.Text.Encoding.UTF8; //添加附件
if (paths.Count != 0)
{
foreach (string path in paths)
{
Attachment data = new Attachment(path, System.Net.Mime.MediaTypeNames.Application.Octet);
message.Attachments.Add(data);
}
} MessageBox.Show("1");
System.Threading.Thread.Sleep(5000);
// MessageBox.Show("2"); try
{
client.Send(message);
All.success++;
All.runing--; }
catch (Exception ex)
{
All.fail++;
All.runing--;
} }
{
string senderStr = alist[j].ToString();
string[] str = senderStr.Split(new char[] { ',', ',',',' });
string stmpStr = str[1];
string fromStr = str[2];
string pwdStr = str[3];
string toMail = str[0];
mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths);
Thread mythread = new Thread(new ThreadStart(mu.SendMail2));
mythread.Start(); }
可以改为:
private ArrayList _alist;
private void time(object source, System.Timers.ElapsedEventArgs e)
{
string senderStr = _alist[0].ToString();
_alist.RemoveAt(0);
if(_alist.Count == 0) ((System.Timers.Timer)source).Close();
string[] str = senderStr.Split(new char[] { ',', ',',',' });
string stmpStr = str[1];
string fromStr = str[2];
string pwdStr = str[3];
string toMail = str[0];
mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths);
mu.SendMail2()
}
_alist = alist;
System.Timers.Timer timer = new System.Timers.Timer(5 * 1000);//5秒
timer.Elapsed += new System.Timers.ElapsedEventHandler(time);
timer.Enabled = true;
timer.Start();
//用timer控件也可以
//全局变量
int i = 0;
//timer的click事件里:
for (int j = i; j < alist.Count; j++)
{
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
i++;
break;
}
{
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
Thread.Sleep(1000);
}
{
string senderStr = alist[j].ToString();
string[] str = senderStr.Split(new char[] { ',', ',',',' });
string stmpStr = str[1];
string fromStr = str[2];
string pwdStr = str[3];
string toMail = str[0];
mailunit mu = new mailunit(stmpStr, fromStr, pwdStr, toMail, t_subject.Text, t_body.Text, paths);
Thread mythread = new Thread(new ThreadStart(mu.SendMail2));
mythread.Start();
if(j>0)mythread.Sleep(5000);
}
这样既简单有快捷的解决了你点击没反映的情况 自己试验下吧
因为当你掉用则个函数的时候 他就会触发睡眠5秒,不是点了没反应 是要等待5秒
就这样加了以后第一次等待去掉 就好了
Application.DoEvents()。
再次感谢大家的帮助!