// Background Worker - run operations private void EventLoadBackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { try { bool auxCancel = false; BackgroundWorker bw = (BackgroundWorker)sender; // input arguments Dictionary<string, object> argsBkWorkerEvents = (Dictionary<string, object>)e.Argument; string openLogFile = (string)argsBkWorkerEvents["OpenLogFile"]; string openConfigFile = (string)argsBkWorkerEvents["OpenConfigFile"]; string defaultPath = (string)argsBkWorkerEvents["DefaultPath"]; SiebelTreeView stv = new SiebelTreeView(openLogFile, openConfigFile, defaultPath); // start events processing stv.LoadEvents(); // wait to event load ends while (stv.AreEventsLoading()) { Thread.Sleep(100); // if is to stop dont talk to load events class anymore if (auxCancel) continue; if (bw.CancellationPending) { // send signal to load events thread end stv.StopEventsLoading(); auxCancel = true; } bw.ReportProgress(stv.LoadEventsPercentageDone()); } // if shit appends then set percentage to 0 and set the results to null if (bw.CancellationPending) { bw.ReportProgress(0); e.Result = null; return; } // if not canceled returns 100% and set the results to the class that represents the event nodes bw.ReportProgress(100); e.Result = stv; } catch (Exception ex) { MessageBox.Show(ex.Message, @"Load Events to TreeView", MessageBoxButtons.OK, MessageBoxIcon.Error); e.Result = null; } }