2012-10-01 35 views
1

所以即时尝试推迟从我的程序发送电子邮件。 为了保持UI的交互性,我开始了一个新线程,并在线程中调用了email方法。有用。它发送电子邮件。但我不知道如何睡觉的线程。 我试着在实际的电子邮件方法中使用Thread.sleep(),但它似乎没有工作。睡觉一个新的线程c#

Thread oThread = new Thread(new ThreadStart(() => { sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody); })); 
       oThread.Start(); 

电子邮件方式..

public void sendEMailThroughOUTLOOK(string recipient, string subject, string body) 
    { 
     Thread.Sleep(60000); 

     try 
     { 

      // Create the Outlook application. 
      Outlook.Application oApp = new Outlook.Application(); 
      // Create a new mail item. 
      Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
      // Set HTMLBody. 
      //add the body of the email 
      oMsg.Body = body; 

      oMsg.Subject = subject; 
      // Add a recipient. 
      Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
      // Change the recipient in the next line if necessary. 
      Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient); 
      oRecip.Resolve(); 
      // Send. 
      oMsg.Send(); 
      // Clean up. 
      oRecip = null; 
      oRecips = null; 
      oMsg = null; 
      oApp = null; 



     }//end of try block 
     catch (Exception ex) 
     { 
     }//end of catch 

     //end of Email Method 
    } 
+2

也许这是一个愚蠢的问题,但为什么你想要线程睡觉? –

+3

你有没有看过使用计时器? –

+0

在调试器中逐步完成。我猜你可以自己找到这个问题。这是一项重要的学习技能。 – usr

回答

1

没有什么明显错你发布的代码。但是,thread.Suspend()是一个非常旧的API =自.NET 2.0以来它已被弃用/废弃,因为这样做不安全。

静态方法Thread.Sleep(N)肯定会将调用线程挂起N毫秒。

澄清;调用Thread.Sleep挂起调用线程,所以在你的示例代码中,你有;

public void sendEMailThroughOUTLOOK(string recipient, string subject, string body) 
{ 
    Thread.Sleep(60000); 
    ... 
} 

对Thread.Sleep(60000)的调用挂起正在执行sendEMailThroughOUTLOOK方法的线程。因为你似乎是在自己的线程中调用该方法,正如所证明的那样;

Thread oThread = new Thread(new ThreadStart(() => { sendEMailThroughOUTLOOK(recipientAddress, subjectLine, finalbody); })); 
oThread.Start(); 

正确的线程应该被挂起。

有没有办法做这样的事情;

Thread t = new Thread(); 
t.Start(); 
t.Sleep(60000); 

您可以启动或杀死正在运行的线程,但不会休眠/挂起它。如上所述 - 此API已被弃用,因为它不是实现线程同步的安全方式(请参阅http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend%28v=vs.71%29.aspx以解释为什么这不是一个好主意)。

+0

即时通讯使用visual studio 2012.我可以使用Thread.Sleep(),但我真正需要做的是oThread.sleep。 oThread是我用来发送电子邮件的线程,第一行代码 – Stonep123

+0

是啊。我的意思是看代码,将thread.sleep放入发送电子邮件的方法中,睡眠当前线程。因为我在新线程中调用了这个方法......我认为它会睡1分钟......但电子邮件根本不会发送。我感到困惑 – Stonep123

+0

电子邮件不发送!=线程不睡觉。在休眠线上放一个断点,在后一行放一个断点。然后,你可以得到一个公平的想法,该线程正在睡60s。一旦你确认了,你可以继续研究为什么电子邮件没有发送。 –

0

由于您对编程有点新颖,我不会尽力解释所有相关的细节,但我认为很酷的是,您向我展示了一个与您的示例有关的新技巧,所以如果可以的话我想帮助您。

这不是只有这种方式来编码,但我可以告诉你,这是一个合理的。它使用后台工作组件在发送电子邮件时保持UI的响应。请注意使用后台工作人员提供的事件DoWork和RunWorkerCompleted。这些事件的设置发生在设计师这就是为什么我zipped the solution up for you所以你可以看看整个事情(我这样做与谷歌驱动器,这是我第一次尝试做一个公共共享 - 如果链接打开为你喜欢它为我做,你会得到一个文件菜单,您可以从中选择下载)。

我创建了一个内部私有类并将其传递给后台工作者。我这样做是因为我不想从运行在不同线程中的代码访问我的UI组件中的数据。这并不总是会引起问题,但我认为这是一个很好的做法。另外,如果我想稍后重构代码,它会使它更容易从DoWork中取出这些行,并将它们放在其他地方,而不会大惊小怪。

在多线程编程的更普遍领域 - 这是一个多面的主题,你不需要马上得到它。 This是我最喜欢的教程(这本书也很棒)。

using System; 
using System.ComponentModel; 
using System.Windows.Forms; 
using Outlook = Microsoft.Office.Interop.Outlook; 

namespace Emailer 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void SendButton_Click(object sender, EventArgs e) 
     { 
      this.sendEmailBackgroundWorker.RunWorkerAsync(new _Email 
      { 
       Recipient = this.recipientTextBox.Text, 
       Subject = this.subjectTextBox.Text, 
       Body = this.emailToSendTextBox.Text 
      }); 
     } 

     private class _Email 
     { 
      public string Body { get; set; } 
      public string Subject { get; set; } 
      public string Recipient { get; set; } 
     } 

     private void sendEmailBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) 
     { 
      var email = (_Email)e.Argument; 

      try 
      { 
       Outlook.Application oApp = new Outlook.Application(); 
       Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); 
       oMsg.Body = email.Body; 
       oMsg.Subject = email.Subject; 
       Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; 
       Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(email.Recipient); 
       oRecip.Resolve(); 
       oMsg.Send(); 
       oRecip = null; 
       oRecips = null; 
       oMsg = null; 
       oApp = null; 
      } 
      catch (Exception ex) 
      { 
       e.Result = ex; 
      } 

      e.Result = true; 
     } 

     private void sendEmailBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      string message; 

      if (e.Result is Exception) 
       message = "Error sending email: " + (e.Result as Exception).Message; 
      else if (e.Result is bool && (bool)e.Result) 
       message = "Email is sent"; 
      else 
       throw new Exception("Internal Error: not expecting " + e.Result.GetType().FullName); 

      MessageBox.Show(message); 
     } 
    } 
}