for (int j = 0; j < alist.Count; j++)
            {
                Thread mythread = new Thread(new ThreadStart(mu.add));
                mythread.Start();
            }
如何实现每循环一次暂停几秒再继续执行呢?

解决方案 »

  1.   

    System.Threading.Thread.Sleep(5000);//进程休眠5秒!
      

  2.   

    加在哪里呢?mythread.Start();下面吗?
      

  3.   


     public void SendMail2()
            {
                All.runing++;
             
                try
                {
                    add1();                
                    All.success++;
                    All.runing--;            }
                catch (Exception ex)
                {
                    All.fail++;
                    All.runing--;
                }                     }具体加在哪里呢?
      

  4.   

    参考一下代码,控制台程式        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());
            }
      

  5.   

    Thread.Sleep(int i) //i的单位是1/1000秒
      

  6.   

    Thread.Sleep()方法,可以的,如果控件台程序,也可以用TIMER控件,
      

  7.   

    在你 mm.add()方法 中
    加入 Thread.Sleep(i)(i为要暂停的毫秒数)
      

  8.   

     Thread mythread = new Thread(new ThreadStart(mu.add));
     mythread.Start();
     Thread.Sleep(5000);
      

  9.   

    sleep 决对没有错的吧!!
      

  10.   

    sleep就可以了。
    也可以用timer,事件里什么都不做。。
      

  11.   

                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()
            {
                //...
            }
      

  12.   

    也可以这样:
                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()
            {
                //...
            }
      

  13.   

    System.Threading.Thread.Sleep(要暂停的毫秒数);
      

  14.   

    Thread mythread =Thread.CurrentThread;
    mythread.sleep(3000);
      

  15.   

    用sleep就可以了啊 怎么会不可以呢   
      

  16.   

     是 windows from 的话 加个 timer!
     
         在timer_Click事件中运行 你要循环的代码,设置好时间等
      

  17.   

    嗯,是winform程序,有个button clike 事件触发那段程序。
      

  18.   

    看来大家都推荐sleep ,但是我加上之后会出现程序未响应的情况,这是什么原因呢?
      

  19.   

    用一个timer控件,把代码都放在Timer1_Tick事件里。
    protected void Timer1_Tick(object sender, EventArgs e)
    {
       //这里是你要执行的代码段   System.Threading.Thread.Sleep(要暂停的毫秒数); }
      

  20.   

    我再把该部分完整代码贴出来,希望大家在此基础上改:下面是主窗体程序部分:       /*群发*/
            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--;
                }                     }
      

  21.   

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

  22.   


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

  23.   

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

  24.   

    还是多线程,新的线程由Timer触发,只是这个new和Start由Timer代理了.
      

  25.   

    你这里的Sleep是休眠的主线程...不停的new 新线程。。有响应就诡异了
      

  26.   

           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秒
    就这样加了以后第一次等待去掉 就好了  
      

  27.   

    肯定是放到一个button_click里面了。一点,一sleep,窗口就停止响应了。用appliction.DoEvent()配合一下吧。
      

  28.   

    多谢大家,问题已经解决,其实sleep是可以的,只不过窗体假死而已,为了避免窗体假死的情况,可以使用
    Application.DoEvents()。
    再次感谢大家的帮助!