-
Notifications
You must be signed in to change notification settings - Fork 56
/
EventObjects.cs
41 lines (35 loc) · 1.18 KB
/
EventObjects.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Net.Mail;
using System.Threading;
namespace Synchronization
{
static class EventObjects
{
static void LogFailure(string message, string mailServer)
{
var email = new SmtpClient(mailServer);
using (var emailSent = new ManualResetEvent(false))
{
object sync = new object();
bool tooLate = false; // Prevent call to Set after a timeout
email.SendCompleted += (s, e) =>
{ lock (sync) { if (!tooLate) { emailSent.Set(); } } };
email.SendAsync("logger@example.com", "sysadmin@example.com",
"Failure Report", "An error occurred: " + message, null);
LogPersistently(message);
if (!emailSent.WaitOne(TimeSpan.FromMinutes(1)))
{
LogPersistently("Timeout sending email for error: " + message);
}
lock (sync)
{
tooLate = true;
}
}
}
private static void LogPersistently(string message)
{
// It's only a demo.
}
}
}