-1

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.

Kanut777
  • 7
  • 3

1 Answers1

1

I don't think you can use progress bar as it is unknown how long this query will run. But you can say show an hourglass or spinning image while it executes in background. like this:

MySpinningImage.Visible = true;
Task.Run(t => ExecuteNonQuery("transactMysql")).ContinueWith(t => MySpinningImage.Visible = false)

UPD: As I was corrected below, you still can use a progressbar. It doesn't change much the approach I used, below is updated version:

MyProgressBar.MarqueeAnimationSpeed = 30;
MyProgressBar.Style = ProgressBarStyle.Marquee;
MyProgressBar.Visible = true;
Task.Run(t => ExecuteNonQuery("transactMysql")).ContinueWith(t => MyProgressBar.Visible = false)
tgralex
  • 794
  • 4
  • 14
  • 3
    `ProgressBarStyle.Marquee` – LarsTech Aug 27 '19 at 21:09
  • I see. It could be set on and off the same way as I showed above. I can update. – tgralex Aug 27 '19 at 21:20
  • Mmm, the problem is that ExecuteNonQuery() is on DataAccess layer but I guess I have to put ValidateList on this: Task.Run(t => ValidateList()).ContinueWith(t => MyProgressBar.Visible = false) I think? – Kanut777 Aug 27 '19 at 21:43
  • ExecuteNonQuery(string) is your own method, inside if which you call ExecuteNonQuery() of your SQL command. – tgralex Aug 28 '19 at 01:50