private void BG_DoWork(BackgroundWorker worker, String name) { SQLConnector conn = connectToSQL(); //create connection outside of loop for more reliable operation while (i < filesToRestore.Length) { lock (key) i++; if (i < filesToRestore.Length) //perform this check here instead of inside lock to save on lock time { restoreDatabase(i, filesToRestore, conn, name); int percentComplete = (int)((float)i / (float)(filesToRestore.Length) * 100); worker.ReportProgress(percentComplete); } else if (i == filesToRestore.Length) //when finished with restoring all files, check to make sure they succeeded { stuckRestoringCheck(filesToRestore, conn); } } // Clean up SQLConnector. conn.Dispose(); conn = null; }