Web application Task Parallel threads - c#

I wrote a simple application that sends out bulk emails. It uses a parallel foreach loop to send emails via system.net.mail. The parallel foreach loop is surrounded by a for loop which was chunking up a 15000 email list into 20 each and waiting 1 second by a thread.sleep. I set the executiontimeout to 7200 in the web config. This application should have been done within an hour easy. My question is - why did the application take over a hour to complete. Is it waiting for the threads to complete or something? How can I make it stop? It eventually timed out through another timer - but??? I am new to threading - I am having a hard time trying to wrap my head around this one.
var chunk = emailList.Select((value, index) => new { Index = index, Value = value })
.GroupBy(x => x.Index / Convert.ToInt32(chunkSize))
.Select(g => g.Select(x => x.Value).ToList())
.ToList();
for (int i = 0; i < chunk.Count; i++)
{
if (i > 0)
{
if (!String.IsNullOrEmpty(waitTime))
{
//mre.WaitOne(Convert.ToInt32(waitTime));
//mre.Reset();
Thread.Sleep(Convert.ToInt32(waitTime));
}
}
Parallel.ForEach(chunk[i], email =>
{
try
{
MailMessage em = new MailMessage();
em.From = new MailAddress(tbEmailFrom.Text);
em.To.Add(email.ToString().Trim());
if (!String.IsNullOrEmpty(tbEmailCC.Text))
{
em.CC.Add(tbEmailCC.Text);
}
if (!String.IsNullOrEmpty(tbEmailBC.Text))
{
em.Bcc.Add(tbEmailBC.Text);
}
em.ReplyToList.Add(tbReplyTo.Text);
em.Body = tbEmailBody.Text;
em.IsBodyHtml = true;
em.Subject = tbSubject.Text;
em.Priority = System.Net.Mail.MailPriority.Normal;
em.BodyEncoding = System.Text.Encoding.UTF8;
using (SmtpClient sm = new SmtpClient())
{
if (String.IsNullOrEmpty(smtpServer))
{
sm.Host = Host.SMTPServer;
}
else
{
sm.UseDefaultCredentials = false;
sm.Host = smtpServer;
sm.Credentials = new System.Net.NetworkCredential(smtpUsername, smtpPassword);
}
sm.Send(em);
}
Interlocked.Increment(ref count);
}
catch (SmtpException smtp)
{
errorList.Add(email.ToString() + "<br />");
}
catch (Exception ex)
{
errorList.Add(email.ToString() + "<br />");
Exceptions.LogException(ex);
}
});
}

Related

How to update DataGrid table after some time

I'm working on an application that reads emails, extracts data from them and prints them in DataGrid (as alerts or notifications). The data is stored in a DB.
Here's the scenario:
A new email with data arrives, if a new alert does not arrive within a specific time slot(say within 5 minutes) from the same customer, with the opposite result(e.g. 1st alert was FAIL, 2nd SUCCESS => negation), the alert in the Datagrid is just updated.
Where is my problem:
In the part where I want to update the alert, a new alert is added, which causes a duplicate and the table is confused. And it does not update when new negation alert arrives.
NOTICE:
My application is larger, to better demonstrate my problem, the example is in the consol application.
But with a few modifications, the code is the same
Here is my code:
Method, which read and extract data
public void MailKitLib(EmailParser emailParser)
{
using (var client = new ImapClient())
{
using (var cancel = new CancellationTokenSource())
{
client.Connect(emailParser.ServerName, emailParser.Port, emailParser.isSSLuse,
cancel.Token);
client.Authenticate(emailParser.Username, emailParser.Password, cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Unread);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
MyAlert alert = new MyAlert(message.MessageId, message.Date.DateTime, message.Subject, message.Subject);
if (!MyAlert.alerts.Any(x => x.Id.Equals(alert.Id)))
{
MyAlert.alerts.Add(alert);\\Think of it as a method that saves the object to the database
}
CheckForUpdates(MyAlert.alerts[i]);
}
}
client.Disconnect(true);
}
}
Method checking for updates
public void CheckForUpdates(MyAlert alert)
{
double result = 0;
foreach (var item in MyAlert.alerts.Select(x => x.NameOfCustomer.Equals(alert.NameOfCustomer)))
{
if (item)
{
for (int i = 0; i < MyAlert.alerts.Count(); i++)
{
result = MyAlert.alerts[i].Date.Minute - alert.Date.Minute;
Console.WriteLine("Result of:" + MyAlert.alerts[i].NameOfCustomer + " " +
"and" + " " + alert.NameOfCustomer + " " + "is:" + result);
if (result > 0 && result <= 5)
{
Console.WriteLine("You HAVE TO UPDATE this" + " " + MyAlert.alerts[i].NameOfAlert);
MyAlert.alerts[i].NameOfAlert = "UPDATED";
break;
}
else
{
Console.WriteLine("You DON'T have to UPDATE this:" + " " + MyAlert.alerts[i].NameOfAlert);
}
}
}
}
Console.WriteLine("Wave:" + MyAlert.alerts.Count);
}
This is how I figured this out with DB
public double GetInterval(DateTime a, DateTime b)
{
return a.Subtract(b).TotalMinutes;
}
public void FindUpdate(Alert newAlert, Alert matchAlert)
{
//If new Alerts arrives from same customer with same problem till five 5 minutes
if (dAOAlert.GetAll().Any(x => x.Email.Equals(newAlert.Email) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert))
&&
GetInterval(newAlert.Date, matchAlert.Date) > 0 && GetInterval(newAlert.Date, matchAlert.Date) <= 5)
{
//Find that Alert, and update his variables from alert, else save new alert
var item = dAOAlert.GetAll().FirstOrDefault(x => x.Email.Equals(newAlert.Email) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert));
dAOAlert.Update(item, newAlert);
dAOAlert.Delete(newAlert);
LoadAlertGrid();
}
}
And this is how I have used this in Main method
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
GetBodyText = message.TextBody;
Problem problem = new Problem(message.MessageId);
if (!dAOProblem.GetAll().Any(x => x.Message_Id.Equals(problem.Message_Id)))
{
dAOProblem.Save(problem);
Alert alert = new Alert(message.MessageId, message.Date.DateTime, message.From.ToString(), 1, problem.Id);
if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
{
dAOAlert.Save(alert);
foreach (var item in dAOAlert.GetAll())
{
FindUpdate(alert, item);
}
LoadAlertGrid();
}
else
{
MessageBox.Show("Duplicate");
}
}
}

Trying to run 100 parallel tasks on an iteration doesn't work

Well, I'm trying to run a task 100 times on each run (with paralellism) but I can't manage this to work.
I'm trying to bruteforce an API, for this the API allows me to concatenate as many IDS as possible (without exceeding the timeout)
// consts:
// idsNum = 1000
// maxTasks = 100
// We prepare the ids that we will process (in that case 100,000)
var ids = PrepareIds(i * idsNum * maxTasks, idsNum, maxTasks);
// This was my old approach (didn't work)
//var result = await Task.WhenAll(ids.AsParallel().Select(async x => (await client.GetItems(x)).ToArray()));
// This is my new approach (also this didn't worked...)
var items = new List<item[]>();
ids.AsParallel().Select(x => client.GetItems(x).GetAwaiter().GetResult()).ForAll(item =>
{
//Console.WriteLine("processed!");
items.Add(item.ToArray());
});
var result = items.ToArray();
As you can see I put Console.WriteLine("processed!"); statment, in order to check if anything worked... But I can't manage this to work.
Those are my other methods:
private static IEnumerable<ulong[]> PrepareIds(int startingId, int idsNum = 1000, int maxTasks = 100)
{
for (int i = 0; i < maxTasks; i++)
yield return Range((ulong)startingId, (ulong)(startingId + idsNum)).ToArray();
}
And...
public static async Task<IEnumerable<item>> GetItems(this HttpClient client, ulong[] ids, Action notFoundCallback = null)
{
var keys = PrepareDataInKeys(type, ids); // This prepares the data to be sent to the API server
var content = new FormUrlEncodedContent(keys);
content.Headers.ContentType =
new MediaTypeHeaderValue("application/x-www-form-urlencoded") { CharSet = "UTF-8" };
client.DefaultRequestHeaders.ExpectContinue = false;
// We create a post request
var response = await client.PostAsync(new Uri(FILE_URL), content);
string contents = null;
JObject jObject;
try
{
contents = await response.Content.ReadAsStringAsync();
jObject = JsonConvert.DeserializeObject<JObject>(contents);
}
catch (Exception ex)
{
Console.WriteLine(ex);
Console.WriteLine(contents);
return null;
}
// Then we read the items from the parsed JObject
JArray items;
try
{
items = jObject
.GetValue("...")
.ToObject<JObject>()
.GetValue("...").ToObject<JArray>();
}
catch (Exception ex)
{
Console.WriteLine(ex);
return null;
}
int notFoundItems = 0;
int nonxxxItems = 0;
int xxxItems = 0;
var all = items.BuildEnumerable(notFoundCallback, item =>
{
if (item.Result != 1)
++notFoundItems;
else if (item.id != 5)
++nonxxxItems;
else
++xxxItems;
});
CrawledItems += ids.Length;
NotFoundItems += notFoundItems;
NonXXXItems += nonxxxItems;
XXXItems += xxxItems;
return all;
}
private static IEnumerable<item> BuildEnumerable(this JArray items, Action notFoundCallback, Action<item> callback = null)
{
foreach (var item in items)
{
item _item;
try
{
_item = new item(item.ToObject<JObject>());
callback?.Invoke(_item);
}
catch (Exception ex)
{
if (notFoundCallback == null)
Console.WriteLine(ex, Color.Red);
else
notFoundCallback();
continue;
}
yield return _item;
}
}
So as you can see I create 100 parallel post requests using an HttpClient. But I can't manage it to work.
So the thing that I want to achieve is to retrieve as many items as possible because I need to crawl +2,000,000,000 items.
But any breakpoint is triggered, neither any caption is updated on Console (I'm using Konsole project in order to print values at a fixed position on console), so any advice can be given there?

Sending email with EWS API very slow

My code below is performing very poorly. I used the .Net StopWatch class to find out what piece of code was causing the slowness.
This line seems to be the issue:
message.SendAndSaveCopy();
E.G batch of 20 emails: 1st email takes around 2 seconds to send and this time gradually increases until the 20th email, which takes up-to 18 seconds.
public int SendBulkMarketing(bool CheckDelivery)
{
int iCounter = 0;
DataTable dt = null;
try
{
DeliveryReportDAL myDeliveryReportDAL = new DeliveryReportDAL();
dt = myDeliveryReportDAL.GetListMessageToSend('E');
if (dt == null) return iCounter;
iCounter = dt.Rows.Count;
}
catch (Exception ex)
{
new Util().LogError(ex, "SMTP:: SendBulkMarketing 1st catch");
return 0;
}
if (iCounter > 0)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials(Account_UserName, Account_Password, Account_Domain);
service.Url = new Uri(Service_URL);
for (int I = 0; I < iCounter; ++I)
{
try
{
string myGUID = "{" + dt.Rows[I]["GUID"].ToString() + "}";
if (IsValidEmailAddress(dt.Rows[I]["OwnerEmail"].ToString()) == false)
{
DeliveryReportDAL myReport = new DeliveryReportDAL();
myReport.SaveSentStatus(myGUID, 'G', 3);
continue;
}
EmailMessage message = new EmailMessage(service);
message.Subject = dt.Rows[I]["TemplateSubject"].ToString();
message.Body = dt.Rows[I]["TemplateText"].ToString().Replace("\0", " ");
message.ToRecipients.Add(dt.Rows[I]["OwnerEmail"].ToString());
message.IsDeliveryReceiptRequested = true;
Guid myPropertySetId = new Guid(myGUID);
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "blablabla", MapiPropertyType.String);
ServicePointManager.ServerCertificateValidationCallback =
delegate(object sender1,
System.Security.Cryptography.X509Certificates.X509Certificate certificate,
System.Security.Cryptography.X509Certificates.X509Chain chain,
System.Net.Security.SslPolicyErrors sslPolicyErrors)
{ return true; };
message.SendAndSaveCopy();
DeliveryReportDAL myReport1 = new DeliveryReportDAL();
myReport1.SaveSentStatus(dt.Rows[I]["GUID"].ToString(), 'G', 1);
}
catch (Exception ex)
{
new Util().LogError(ex, "SMTP:: SendBulkMarketing 2nd catch");
}
}
}
return iCounter;
}
I would greatly appreciate any help in improving the performance of my code.
This performance issue is now resolved.
It was a fundamental coding problem. The issue was caused by a separate method call which was being called (not shown in the code snippet provided) inside the for loop. It contains performance heavy functionality, but it did not need to be called inside the for loop.
Moving this performance heavy method call outside of the for loop resolved the issue.

Getting The Wait Operation Timeout Exception for a query from Skip Value 100

I am writing a small data migration tools from one big database to another small database. All of the others data migration method worked satisfactorily, but the following method has given an exception from the SKIP VALUE IS 100. I run this console script remotely as well as inside of the source server also. I tried in many different was to find the actual problem what it is. After then I found that only from the SKIP VALUE IS 100 it is not working for any TAKE 1,2,3,4,5 or ....
Dear expertise, I don't have any prior knowledge on that type of problem. Any kind of suggestions or comments is appreciatable to resolve this problem. Thanks for you time.
I know this code is not clean and the method is too long. I just tried solve this by adding some line of extra code. Because the problem solving is my main concern. I just copy past the last edited method.
In shot the problem I can illustrate with this following two line
var temp = queryable.Skip(90).Take(10).ToList(); //no exception
var temp = queryable.Skip(100).Take(10).ToList(); getting exception
private static void ImporterDataMigrateToRmgDb(SourceDBEntities sourceDb, RmgDbContext rmgDb)
{
int skip = 0;
int take = 10;
int count = sourceDb.FormAs.Where(x=> x.FormAStateId == 8).GroupBy(x=> x.ImporterName).Count();
Console.WriteLine("Total Possible Importer: " + count);
for (int i = 0; i < count/take; i++)
{
IOrderedQueryable<FormA> queryable = sourceDb.FormAs.Where(x => x.FormAStateId == 8).OrderBy(x => x.ImporterName);
List<IGrouping<string, FormA>> list;
try
{
list = queryable.Skip(skip).Take(take).GroupBy(x => x.ImporterName).ToList();
//this line is getting timeout exception from the skip value of 100.
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
sourceDb.Dispose();
rmgDb.Dispose();
sourceDb = new SourceDBEntities();
rmgDb = new RmgDbContext();
skip += take;
continue;
}
if (list.Count > 0)
{
foreach (var l in list)
{
List<FormA> formAs = l.ToList();
FormA formA = formAs.FirstOrDefault();
if (formA == null) continue;
Importer importer = formA.ConvertToRmgImporterFromFormA();
Console.WriteLine(formA.FormANo + " " + importer.Name);
var importers = rmgDb.Importers.Where(x => x.Name.ToLower() == importer.Name.ToLower()).ToList();
//bool any = rmgDb.Importers.Any(x => x.Name.ToLower() == formA.ImporterName.ToLower());
if (importers.Count() == 1)
{
foreach (var imp in importers)
{
Importer entity = rmgDb.Importers.Find(imp.Id);
entity.Country = importer.Country;
entity.TotalImportedAmountInUsd = importer.TotalImportedAmountInUsd;
rmgDb.Entry(entity).State = EntityState.Modified;
}
}
else
{
rmgDb.Importers.Add(importer);
}
rmgDb.SaveChanges();
Console.WriteLine(importer.Name);
}
}
skip += take;
}
Console.WriteLine("Importer Data Migration Completed");
}
I have fixed my problem by modifying following code
var queryable =
sourceDb.FormAs.Where(x => x.FormAStateId == 8)
.Select(x => new Adapters.ImporterBindingModel()
{
Id = Guid.NewGuid().ToString(),
Active = true,
Created = DateTime.Now,
CreatedBy = "System",
Modified = DateTime.Now,
ModifiedBy = "System",
Name = x.ImporterName,
Address = x.ImporterAddress,
City = x.City,
ZipCode = x.ZipCode,
CountryId = x.CountryId
})
.OrderBy(x => x.Name);

How to create async http requests in a loop?

Yesterday I've found out how to create several async http requests without async/await. But today I need to do it in a loop: if some of responses don't satisfy some condition - I need to change a request for them and send these requests again. It may be repeated several times.
I've tried this code:
do
{
var loadingCoordinatesTasks = new List<Task<Terminal>>();
var totalCountOfTerminals = terminalPresetNode.ChildNodes.Count;
var uiTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
foreach (var terminal in terminals.Except(_terminalsWithCoordinates))
{
var address = terminal.GetNextAddress();
var webRequest = (HttpWebRequest)WebRequest.Create(GeoCoder.GeoCodeUrl + address);
var webRequestTask = Task.Factory.FromAsync<WebResponse>(webRequest.BeginGetResponse,
webRequest.EndGetResponse,
terminal);
var parsingTask = webRequestTask.ContinueWith(antecedent =>
{
// Parse the response
});
loadingCoordinatesTasks.Add(parsingTask);
}
Task.Factory.ContinueWhenAll(loadingCoordinatesTasks.ToArray(), antecedents =>
{
foreach (var antecedent in antecedents)
{
var terminalWithCoordinates = antecedent.Result;
if (antecedent.Status == TaskStatus.RanToCompletion &&
!terminalWithCoordinates.Coordinates.AreUnknown)
{
_terminalsWithCoordinates.Add(terminalWithCoordinates);
_countOfProcessedTerminals++;
}
}
});
} while (_countOfProcessedTerminals < totalCountOfTerminals);
but is it possible to check the condition in while just after every single set of requests executed?
You can perform the check after increasing the count:
_countOfProcessedTerminals++;
if (_countOfProcessedTerminals >= totalCountOfTerminals)
{
break;
}
Is _countOfProcessedTerminals thread-safe though?
I manage to do it using recursion:
public void RunAgainFailedTasks(IEnumerable<Task<Terminal>> tasks)
{
Task.Factory.ContinueWhenAll(tasks.ToArray(), antecedents =>
{
var failedTasks = new List<Task<Terminal>>();
foreach (var antecedent in antecedents)
{
var terminal = antecedent.Result;
// Previous request was failed
if (terminal.Coordinates.AreUnknown)
{
string address;
try
{
address = terminal.GetNextAddress();
}
catch (FormatException) // No versions more
{
continue;
}
var getCoordinatesTask = CreateGetCoordinatesTask(terminal, address);
failedTasks.Add(getCoordinatesTask);
}
else
{
_terminalsWithCoordinates.Add(terminal);
}
}
if (failedTasks.Any())
{
RunAgainFailedTasks(failedTasks);
}
else
{
// Display a map
}
}, CancellationToken.None,
TaskContinuationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext());
}
private Task<Terminal> CreateGetCoordinatesTask(Terminal terminal, string address)
{
var webRequest = (HttpWebRequest)WebRequest.Create(GeoCoder.GeoCodeUrl + address);
webRequest.KeepAlive = false;
webRequest.ProtocolVersion = HttpVersion.Version10;
var webRequestTask = Task.Factory.FromAsync<WebResponse>(webRequest.BeginGetResponse,
webRequest.EndGetResponse,
terminal);
var parsingTask = webRequestTask.ContinueWith(webReqTask =>
{
// Parse the response
});
return parsingTask;
}

Categories

Resources