private void timer4_Tick(object sender, EventArgs e)
{
se.SendPhotos(photofilesDir + "\\" + "photofiles.zip");
if (se.photossendended == true)
{
se.photossendended = false;
timer4.Enabled = false;
timer5.Enabled = true;
}
}
直到se.photossendended == true
它继续使se.SendPhotos(photofilesDir + "\\" + "photofiles.zip");
但我希望它只做一次,并不断检查se.photossendended
它是否为真。所以我试着做while(true)
private void timer4_Tick(object sender, EventArgs e)
{
se.SendPhotos(photofilesDir + "\\" + "photofiles.zip");
while(true)
{
if (se.photossendended == true)
{
se.photossendended = false;
timer4.Enabled = false;
timer5.Enabled = true;
}
}
}
但随后它将保存所有程序并且永远不会实现它,因为程序没有继续并且它全部卡在这个循环中。所以它永远不会是真的,循环将永远保持下去。
编辑**
这是 se 类 SendEmail
public void SendPhotos(string fileNameToSend)
{
try
{
MailAddress from = new MailAddress("username", "User " + (char)0xD8 + " Name",
System.Text.Encoding.UTF8);
MailAddress to = new MailAddress("myrmail");
photosmessage = new MailMessage(from, to);
photosmessage.Body = "Please check the log file attachment i have some bugs.";
string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
photosmessage.Body += Environment.NewLine + someArrows;
photosmessage.BodyEncoding = System.Text.Encoding.UTF8;
photosmessage.Subject = "Log File For Checking Bugs" + someArrows;
photosmessage.SubjectEncoding = System.Text.Encoding.UTF8;
Attachment myAttachment = new Attachment(fileNameToSend, MediaTypeNames.Application.Octet);
photosmessage.Attachments.Add(myAttachment);
SmtpClient photossend = new SmtpClient("smtp.gmail.com", 587);
photossend.SendCompleted += new SendCompletedEventHandler(photossend_SendCompleted);
photossend.EnableSsl = true;
photossend.Timeout = 10000;
photossend.DeliveryMethod = SmtpDeliveryMethod.Network;
photossend.UseDefaultCredentials = false;
photossend.Credentials = new NetworkCredential("user", "pass");
string userState = "test message1";
photossend.SendAsync(photosmessage, userState);
SendLogFile.Enabled = false;
}
catch (Exception errors)
{
Logger.Write("Error sending message :" + errors);
}
}
private void photossend_SendCompleted(object sender, AsyncCompletedEventArgs e)
{
photosmessage.Dispose();
photossendended = true;
}
我想确保发送的电子邮件是真的,所以我这样做了:photosendended = true; 然后在 Timer4 勾选事件中的 Form1 中,如果它真的停止计时器激活计时器 5,我想发送一次电子邮件,然后再发送第二封电子邮件并一遍又一遍。
我有 4 个计时器滴答事件,我可以禁用并一一启用它们。原因是我只想在前一封电子邮件完成发送后才发送每封电子邮件。