所以即時嘗試推遲從我的程序發送電子郵件。 爲了保持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
}
也許這是一個愚蠢的問題,但爲什麼你想要線程睡覺? –
你有沒有看過使用計時器? –
在調試器中逐步完成。我猜你可以自己找到這個問題。這是一項重要的學習技能。 – usr