-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmailThread.cs
217 lines (192 loc) · 6.84 KB
/
EmailThread.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading;
using HDCore;
using HDCPProtocol2;
using Dapper;
using VODServiceBase;
namespace HDVietNamService
{
public class EmailThread : IThreadClass
{
private MainClass Owner;
public EmailThread(MainClass owner)
{
this.Owner = owner;
}
/// <summary>
/// Tên tiến trình
/// </summary>
public string ThreadName { get { return "Send Email"; } }
bool isBusy = false;
/// <summary>
/// Trạng thái tiến trình
/// </summary>
/// <returns></returns>
public bool IsBusy()
{
return isBusy;
}
/// <summary>
/// Hủy 1 hàng trong tiến trình
/// </summary>
/// <param name="index">index của dòng cần dừng</param>
public void CancelIndex(long index) { }
public void RemoveIndex(long index) { }
/// <summary>
/// Ưu tiên hàng trong tiến trình
/// </summary>
/// <param name="index"></param>
public void UuTienIndex(long index) { }
/// <summary>
/// Làm ngay hàng trong tiến trình
/// </summary>
/// <param name="index"></param>
public void LamNgayIndex(long index) { }
bool isRun = false;
/// <summary>
/// Bắt đầu tiến trình
/// </summary>
public void Start()
{
if (!isBusy)
{
isRun = true;
Thread thrEmail = new Thread(SendEmail);
thrEmail.IsBackground = true;
thrEmail.Start();
}
}
/// <summary>
/// Khởi động lại tiến trình
/// </summary>
public void Restart()
{
bool needRestart = isRun;
if (isBusy)
{
this.Stop();
while (isBusy)
Thread.Sleep(100);
}
if (needRestart)
this.Start();
}
/// <summary>
/// Dừng tiến trình
/// </summary>
public void Stop()
{
isRun = false;
Monitor.Enter(lockMail);
Monitor.PulseAll(lockMail);
Monitor.Exit(lockMail);
}
/// <summary>
/// Dừng tiến trình khi kết thúc
/// </summary>
public void StopWhenFinish()
{
Stop();
}
/// <summary>
/// Sự kiện khi tiến trình bắt đầu hoặc dừng
/// </summary>
public event EventHandler OnBusyChanged;
private Queue<HDStation.SYSTEM_MAIL> lstMail = new Queue<HDStation.SYSTEM_MAIL>();
object lockMail = new object();
public void AddEmail(string mess, string title = "")
{
try
{
if (Owner.config.EmailSend != null && Owner.config.EmailSend != "")
{
mess = "[" + DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss") + "]:" + mess + " (Máy " + Utils.GetCurrentIP() + ")";
Monitor.Enter(lockMail);
foreach (var mail in Owner.config.EmailSend.Split(';'))
lstMail.Enqueue(new HDStation.SYSTEM_MAIL()
{
TARGET_MAIL = mail.Trim(),
CONTENT = mess,
DATECREATE = DateTime.Now,
TITLE = Owner.ServiceName + (title == "" ? "" : "_" + title)
});
Monitor.PulseAll(lockMail);
Monitor.Exit(lockMail);
}
}
catch { }
}
private void SendEmail()
{
isBusy = true;
if (OnBusyChanged != null)
OnBusyChanged(this, new EventArgs());
SqlConnection dbEmail = null;
#region Thread
while (isRun)
{
try
{
HDStation.SYSTEM_MAIL firstMail = null;
Monitor.Enter(lockMail);
while (isRun && lstMail.Count <= 0)
Monitor.Wait(lockMail);
if (isRun)
firstMail = lstMail.Dequeue();
Monitor.Exit(lockMail);
if (firstMail != null)
{
if (dbEmail == null && Owner.config.DBStationConnectionString != null && Owner.config.DBStationConnectionString != "")
dbEmail = new SqlConnection(Owner.config.DBStationConnectionString);
if (dbEmail != null)
{
try
{
string mess = firstMail.CONTENT;
while (mess.Length > 0)
{
string mess1 = "";
if (mess.Length > 2000)
{
mess1 = mess.Substring(0, 2000);
mess = mess.Substring(2000);
}
else
{
mess1 = mess;
mess = "";
}
dbEmail.Execute(@"Insert Into SYSTEM_MAIL(TARGET_MAIL, CONTENT, DATECREATE, TITLE)
Values(@TARGET_MAIL, @CONTENT, @DATECREATE, @TITLE)",
new
{
TARGET_MAIL = firstMail.TARGET_MAIL,
CONTENT = mess1,
DATECREATE = firstMail.DATECREATE,
TITLE = firstMail.TITLE
});
}
}
catch(Exception ex)
{
Owner.SendLogToAll("Email thread error:" + ex.Message);
if (!isRun)
break;
Thread.Sleep(100);
}
}
}
}
catch { }
}
#endregion
isBusy = false;
if (OnBusyChanged != null)
OnBusyChanged(this, new EventArgs());
}
}
}