Backgroundworker or easier solution to refresh window messages

  • Thread starter Thread starter sippyuconn
  • Start date Start date
S

sippyuconn

I am have a moment where i am having brain freeze :-)

trying to execute a sql statement that will take awhile but I want to
process the windows messages and force the user to wait for completion. This
code works but the first messagebox doesn't repaint and I get a half painted
window

Would a backgrounder worker be the best option and how to put in so user
needs to wait for completion of sql ???

Thanks


if (MessageBox.Show("Are you sure you want to delete all records ?", "Delete
Records", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
try
{
doCommands();
cmdQuery.DbExecute("DELETE FROM " + myTable);
CloseDB();
MessageBox.Show("Success - deleting records ", "Delete
Records", MessageBoxButtons.OK);
}
catch (Exception ee)
{
Logger.WriteException(ee);
MessageBox.Show("Failure - deleting records ", "Delete
Records", MessageBoxButtons.OK);
}
 
I see a few things that could be improved upon here.

First, do you have to use the DbExecute method? That kind of method can
be very dangerous, and open you up to injection attacks (since it relies on
you to create the command to execute on the server).

Also, you should have some sort of implementation of IDisposable, so you
can use the using statement, or at the least, have a finally clause where
you place the CloseDB method (as I assume that method closes any resources
that the database is using).

As for making this asynchronous, if you don't have access to the
SqlCommand, then yes, a BackgroundWorker, Thread, or call to the ThreadPool
will be just fine. The BackgroundWorker is probably what you want, since I
assume you want some sort of notification as to when the operation is
complete.

Be careful though, you want to make sure you disable the appropriate
sections of your UI, since I am sure you don't want other operations trying
to be performed while this one is taking place.

Finally, you might want to try a TRUNCATE TABLE statement. It doesn't
write to the transaction log in SQL Server, and will be much quicker. The
only thing you have to worry about is if you have an IDENTITY column, which
will be reset to the seed value for that column.
 
Back
Top