I have a method that stores data on a database and also send an email, but it takes some time. I decided to use a background worker but I don't now how to set DoWork event properly.
I´m doing the same approach on this thread:
How to use a BackgroundWorker?
So I put this method inside DoWork event.
private void ValidateList()
{
resultado = tramite.SaveChanges();
}
The method below is called on the presentation layer with ValidateList.
protected int ExecuteNonQuery(string transactMysql)
{
using (var conexion = GetConnection())
{
conexion.Open();
using (var cmd = new MySqlCommand())
{
cmd.Connection = conexion;
cmd.CommandText = transactMysql;
cmd.CommandType = CommandType.StoredProcedure;
if (cmd.CommandText.Equals("update_delegate") || cmd.CommandText.Equals("delegate_tramite"))
{
var mailService = new MailServices.SystemSupportMail();
mailService.sendMail(
subject: "xxx: xxxxx",
body: "Hola" + SendEmailCache.destinatario,
recipientMail: SendEmailCache.mail
);
}
foreach (MySqlParameter item in parameters)
{
cmd.Parameters.Add(item);
}
int result = cmd.ExecuteNonQuery();
parameters.Clear();
return result;
}
}
}
I expect that the progress bar fills normally according time spend on the execution.