如何让程序暂停几秒再执行

bejco 2009-09-13 12:35:23

for (int j = 0; j < alist.Count; j++)
{
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
}


如何实现每循环一次暂停几秒再继续执行呢?
...全文
2751 63 打赏 收藏 转发到动态 举报
写回复
用AI写文章
63 条回复
切换为时间正序
请发表友善的回复…
发表回复
tys_mylove 2011-05-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 projectdd 的回复:]
Thread.Sleep(int i) //i的单位是1/1000秒
[/Quote]


这个是正确的
bejco 2009-09-17
  • 打赏
  • 举报
回复
多谢大家,问题已经解决,其实sleep是可以的,只不过窗体假死而已,为了避免窗体假死的情况,可以使用
Application.DoEvents()。
再次感谢大家的帮助!
bejco 2009-09-16
  • 打赏
  • 举报
回复
我再把该部分完整代码贴出来,希望大家在此基础上改:

下面是主窗体程序部分:

/*群发*/
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--;
}



}

showjim 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 50 楼 bejco 的回复:]
楼上您这样是不是就不用多线程了?
[/Quote]
还是多线程,新的线程由Timer触发,只是这个new和Start由Timer代理了.
bryht 2009-09-16
  • 打赏
  • 举报
回复
for (int j = 0; j < alist.Count; j++)
{
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
Thread.Sleep(1000);
}

BitCoffee 2009-09-16
  • 打赏
  • 举报
回复

//用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;
}
bejco 2009-09-16
  • 打赏
  • 举报
回复
楼上您这样是不是就不用多线程了?
showjim 2009-09-16
  • 打赏
  • 举报
回复
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();
simen_frankly 2009-09-16
  • 打赏
  • 举报
回复
Thread.Sleep
yutian_01261027 2009-09-16
  • 打赏
  • 举报
回复
Thread.Sleep(5*1000)
aimeast 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 bejco 的回复:]
引用 17 楼 wuyq11 的回复:
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
Thread.Sleep(5000);




这样触发事件之后程序就无法响应了,奇怪的很。
[/Quote]
肯定是放到一个button_click里面了。一点,一sleep,窗口就停止响应了。

用appliction.DoEvent()配合一下吧。
daihaolr 2009-09-16
  • 打赏
  • 举报
回复
学习。
longhair9711 2009-09-16
  • 打赏
  • 举报
回复
ding
cg173619268 2009-09-16
  • 打赏
  • 举报
回复
       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秒
就这样加了以后第一次等待去掉 就好了
xianglf 2009-09-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhoukang0916 的回复:]
C# codeSystem.Threading.Thread.Sleep(5000);//进程休眠5秒!
[/Quote]
这个对的。
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 bejco 的回复:]
引用 17 楼 wuyq11 的回复:
Thread mythread = new Thread(new ThreadStart(mu.add));
mythread.Start();
Thread.Sleep(5000);




这样触发事件之后程序就无法响应了,奇怪的很。
[/Quote]
你这里的Sleep是休眠的主线程...不停的new 新线程。。。。。有响应就诡异了
bejco 2009-09-15
  • 打赏
  • 举报
回复
还是没解决。
robin521 2009-09-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhoukang0916 的回复:]
C# codeSystem.Threading.Thread.Sleep(5000);//进程休眠5秒!
[/Quote]
就是这个了.
24K純帥 2009-09-14
  • 打赏
  • 举报
回复
Sleep()
最菜的鸟 2009-09-14
  • 打赏
  • 举报
回复
又学了点新的东西。学习中……
加载更多回复(43)

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧