private void RunActionThread(Action action) { if (isExecuting == false) { btnCancel.Enabled = true; pnlAfterLogin.Enabled = false; pnlTransactions.Enabled = false; isExecuting = true; blendleParser.Cancel = false; pdfCreator.Cancel = false; backgroundWorker = new BackgroundWorker(); backgroundWorker.DoWork += new DoWorkEventHandler((sender, args) => action.Invoke()); // This does the job ... backgroundWorker.WorkerSupportsCancellation = true; // This allows cancellation. backgroundWorker.RunWorkerCompleted += (sender, args) => { if (args.Cancelled && cbSaveLog.Checked) { //if canceled, the log is propably not save, do it manually GeneralUtils.CheckDataDir(); var @now = DateTime.Now; string logFullPath = Path.Combine(Constants.DEFAULT_DATA_FULLPATH, "log_" + now.ToString("ddMMyyyHHmmss") + now.Millisecond.ToString() + ".txt"); File.WriteAllText(logFullPath, txtLog.Text); } isExecuting = false; pnlAfterLogin.Enabled = true; pnlTransactions.Enabled = true; blendleParser.Cancel = false; btnCancel.Enabled = true; backgroundWorker.Dispose(); }; backgroundWorker.RunWorkerAsync(); } }