forked from ugniusramanauskas/iMacrosTest-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BckWorkerTemplate.cs
180 lines (154 loc) · 6.04 KB
/
BckWorkerTemplate.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
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Configuration;
namespace iMacrosPostingDashboard
{
public class BckWorkerTemplate
{
int totalErr = 0;
private string locProjName = "";
private string locTopicTable = "";
private string locAnswTmpl = "";
private ProjectTableRow localTableRow;
private int locpausebfconfirm;
private int locpausebfnextpost;
private bool locpostQnA;
private List<string> ResultBoxVirtual = new List<string>();
// private int ProgressBarVirtual = 0;
System.ComponentModel.BackgroundWorker bw;
public BckWorkerTemplate()
{
}
public BckWorkerTemplate(string ProjName, string TopicTable, string AnswTmpl, int pausebfconfirm, int pausebfnextpost, bool postQnA, ref ProjectTableRow CntRow)
{
bw = new System.ComponentModel.BackgroundWorker();
locProjName = ProjName;
locTopicTable = TopicTable;
locAnswTmpl = AnswTmpl;
locpausebfconfirm = pausebfconfirm;
locpausebfnextpost = pausebfnextpost;
locpostQnA = postQnA;
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWorkDataGrid);
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompdDataGrid);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChdDataGrid);
localTableRow = CntRow;
// e.g. "MumsNetUK", "topicsmumsnet", "AmazonUK-one-link"
}
private void EmailProjStatus()
{
// READ FROM CONFIG FILE
string fromSender = (string)ConfigurationManager.AppSettings["Sender"];
string toReceiver = (string)ConfigurationManager.AppSettings["ReceiverAdmin"];
string SenderPass = (string)ConfigurationManager.AppSettings["SenderPass"];
// STANDARD SMTP SENDER CODE
var fromAddress = new MailAddress(fromSender, "Forumu Statusas");
var toAddress = new MailAddress(toReceiver, "");
string fromPassword = SenderPass;
string subject5orless = "Pranešimas: forumas " + locProjName + " sustojo";
string subjectNone = "Pranešimas: forumas " + locProjName + " sustojo";
string subject = "Pranešimas: forumas " + locProjName + " sustojo";
string body = @" \n";
// if (Anykwdsleft) subject = subject5orless;
// else subject = subjectNone;
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
smtp.Send(message);
}
}
public void bwStartDataGrid(Object sender, EventArgs e)
{
// btnStart.Enabled = false;
totalErr = 0;
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
// ResultBoxVirtual = new VTextBox();
// ProgressBarVirtual = new VProgressBar();
localTableRow.ProgressReport = "Running..." + Environment.NewLine;
}
}
private void bw_DoWorkDataGrid(Object sender, DoWorkEventArgs e)
{
GenericPoster poster2 = new GenericPoster(ref sender, ref e, locProjName, locTopicTable, locAnswTmpl);
poster2.RunningStatus = true;
if (!locpostQnA)
poster2.RunSimplePoster(locpausebfconfirm, locpausebfnextpost);
/*
int i = 0;
while (!bw.CancellationPending)
{
System.Threading.Thread.Sleep((1000 * 1));
bw.ReportProgress(i, i.ToString());
i++;
if (i > 50) i = 0;
}
e.Cancel = true;
return;
*/
}
private void bw_RunWorkerCompdDataGrid(Object sender, RunWorkerCompletedEventArgs e)
{
EmailProjStatus();
if (e.Error != null)
{
localTableRow.ProgressReport += "Errors occured. Please see log above." + Environment.NewLine;
totalErr++;
}
else
{
if (e.Cancelled)
{
localTableRow.ProgressReport += "Cancelled." + Environment.NewLine;
totalErr++;
}
else
{
// WorkResult result = e.Result as WorkResult;
// if (result.ToString() != null)
localTableRow.ProgressReport += "Process finished." + Environment.NewLine;
totalErr++;
}
}
// btnStart.Enabled = true;
}
private void bw_ProgressChdDataGrid(Object sender, ProgressChangedEventArgs e)
{
localTableRow.ProgressBar = e.ProgressPercentage;
string reportmsg = e.UserState as String;
localTableRow.ProgressReport = reportmsg + Environment.NewLine;
}
public void bwStopDataGrid(Object sender, EventArgs e)
{
if (bw.WorkerSupportsCancellation == true)
{
// Cancel the asynchronous operation.
bw.CancelAsync();
// btnStart.Enabled = true;
}
}
}
}