void Scanner_FormClosing(object sender, FormClosingEventArgs e) { if (isLoadingNow) { // Cancel the asynchronous operation. bgWorker.CancelAsync(); e.Cancel = true; } return; }