2013-01-03 58 views
0

我正在使用Windows应用程序发送邮件给多个用户的项目,但发生的是,当我使用Thread.Join()方法时,它使我的主UI不响应...... 什么我想要的是....我想在单独的线程上运行SMTPClienTSetting()方法,并希望我的主UI保持对其他工作的响应。我想在后台运行此邮件发送活动...请使用线程提示我只有概念.....如何等待线程完成运行?Thread.join使我的主线程无响应?

public void SendMail(dataset ds) 
     { 
          for(inti=0<ds.Tables[0].Rows.Count<i++) 
          { 

           Thread tMail = new Thread(() => { 
            resValue = SMTPClienTSetting(ds, mail); 

           }); 
           tMail.IsBackground = true; 
           tMail.Start(); 
           tMail.Join(); 
           if (resValue == "True") 
           { 
            Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp)); 
            tPopUp.IsBackground = true; 
            this.BeginInvoke((MethodInvoker)delegate{ 
          }); 
            tPopUp.Start(); 
            lblPOpUpInfo = "Message sent successfully to \nAsset Owner : " + AssetOwner + "\n" + "Email : " + RecipientID; 

           } 
           else 
           { 
            Thread tPopUp = new Thread(new ThreadStart(this.ShowMessagePopUp)); 
            tPopUp.IsBackground = true; 
                   this.BeginInvoke((MethodInvoker)delegate{ 
          }); 
            tPopUp.Start(); 
            lblPOpUpInfo = "Message sending failed to \nAsset Owner :" + AssetOwner + "\n" + "Email : " + RecipientID + "!!!\nPlease check your SMTP details and\nInternet Connection..!!!"; 
           } 
         } 
     } 
     #region Function for setting SMTP Client 
       public string SMTPClienTSetting(DataSet ds, MailMessage MailstoSend) 
       { 
        try 
        { 
         SmtpClient objsmtp = new SmtpClient(); 
         NetworkCredential NetAccess = new NetworkCredential(); 

         NetAccess.UserName = ds.Tables[0].Rows[0][0].ToString(); 
         //NetAccess.Password = base64Decode(ds.Tables[0].Rows[0][1].ToString()); 

         NetAccess.Password = genFunc.base64Decode(ds.Tables[0].Rows[0][1].ToString()); 
         objsmtp.Credentials = NetAccess; 

         objsmtp.Host = ds.Tables[0].Rows[0][2].ToString(); 
         objsmtp.Port = Convert.ToInt16(ds.Tables[0].Rows[0][3].ToString()); 

         objsmtp.Send(MailstoSend); 

         //System.Threading.Thread.Sleep(500); 

         return "True"; 
        } 
        catch (NullReferenceException nr) 
        { 
         return nr.Message; 
        } 
        catch (Exception ex) 
        { 
         return ex.Message; 
        } 
       } 
       #endregion 
     } 

    #region Function to show Pop Up window using separate thread 
    public void ShowMessagePopUp() 
    { 
      try 
      { 
       frmHomePopUp homePopUp = new frmHomePopUp(); 
       mailTimer.Elapsed += delegate { mailTimer.Stop(); homePopUp.Close(); }; 
       //mailTimer.Elapsed += this.TimeEvent; 
       mailTimer.Interval=5000; 
       mailTimer.Enabled = true; 
       homePopUp.lblInfo.Text = lblPOpUpInfo; 
       homePopUp.Refresh(); 
       mailTimer.Start(); 
       homePopUp.ShowDialog(); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

    } 
    #endregion 

    public void mailTimer_Elapsed(object source, EventArgs e) 
    { 
     //frmHome home; 
     mailTimer.Stop(); 
     } 
+0

您正在使用哪种版本的C#? –

+1

等待==没有做任何事情==没有反应。别等。 –

+0

C#版本4.0 .. –

回答

3

基本上:不要用Thread.Join; 是什么是块。

适当的做法是在辅助方法结束做任何UI工作

Thread tMail = new Thread(() => { 
    resValue = SMTPClienTSetting(ds, mail); 

    if (resValue == "True") { 
     // blah blah blah 
    } 
    // now: if you need to update any UI state: 
    this.BeginInvoke((MethodInvoker)delegate { 
     someControl.Text = lblPOpUpInfo; // for example 
    }); 
}); 
tMail.IsBackground = true; 
tMail.Start(); 

的呼叫Invoke/BeginInvoke切换回UI线程,基本上工作作为一个回调。

请注意,您可能还想在此处使用ThreadPool而不是Thread

+0

嗨马克,this.BeginInvoke解决了我的问题。但我想做轻微修改。请告诉我如何去做。如果(resValue == True),我需要执行一些更多操作,请告诉我如何去做。请参阅我编辑过的代码..... –

+0

嗨马克,当我得到resValue时,我想要显示lblPopUpInfo.Text在新窗口中持续5秒,并且想要关闭它并再次打开它以显示下一条消息。我想要像ASP.NET中的PopUp窗口那样执行此操作。因此,此开启和关闭问题正在发生...... –

+0

@VishalIPatil真的是一样的问题; “Thread.Sleep(5000)”不会“等待”。您**需要**才能让UI线程快速完成。相反,创建一个持续时间为5秒的“计时器”,当计时器触发时:关闭表格。 –