void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = (BackgroundWorker)sender; //this.quickSearchControl.UpdateSearchStatus() String userText = (string)e.Argument; Search newSearch = new Search(userText); bool previousSearchFound = false; lock (this) { //this.quickSearchControl.Invoke(new MethodInvoker(delegate() //{ // quickSearchControl.UpdateSearchStatus(SearchStatus.Pending); //})); for (int i = previousSearches.Count - 1; i >= 0; i--) { if (previousSearches[i].ParamEquals(newSearch)) { previousSearchFound = true; newSearch = previousSearches[i]; Debug.WriteLine("found exact match in previousSearches"); break; } } if (previousSearchFound == false) { for (int i = previousSearches.Count - 1; i >= 0; i--) { if (previousSearches[i].IsRefinedSearch(newSearch)) { previousSearchFound = true; newSearch.performSearch(previousSearches[i].resultEntries, worker); Debug.WriteLine("Search is refined search"); break; } } } } if (previousSearchFound == false) { newSearch.performSearch(database.RootGroup, worker); } //if (!worker.CancellationPending) //{ lock (this) { this.previousSearches.Add(newSearch); if (!worker.CancellationPending) { SearchStatus status; if (newSearch.resultEntries.Count == 0) { status = SearchStatus.Error; } else { status = SearchStatus.Success; } //this.quickSearchControl.Invoke(new MethodInvoker(delegate() //{ // quickSearchControl.UpdateSearchStatus(status); //})); this.quickSearchControl.Invoke(qsUpdateMethod, status, worker.CancellationPending); } } //} //e.Result = newSearch.resultEntries; // for testing // only update the ListView if there are results if (newSearch.resultEntries.Count != 0) { // using the ListView itself for locking caused problems if (!worker.CancellationPending) { //this.listview.Groups.Clear(); ListViewItem[] items = new ListViewItem[newSearch.resultEntries.Count]; int i = 0; foreach (PwEntry entry in newSearch.resultEntries) { if (worker.CancellationPending) { return; //break; } items[i] = AddEntryToList(entry); i++; } lock (listViewLock) { if (!worker.CancellationPending) { //Control.CheckForIllegalCrossThreadCalls = false; //this.listview.BeginUpdate(); //this.listview.Items.Clear(); //this.listview.Items.AddRange(items); //this.listview.Items[0].Selected = true; //this.listview.EndUpdate(); ////Debug.WriteLine("Setting CheckForIllegalCrossThreadCalls to true"); //Control.CheckForIllegalCrossThreadCalls = true; e.Result = items; } } } } // set Cancel if necessary. Otherwise the completed method won't know about the status. //if (worker.CancellationPending) //{ // e.Cancel = true; //} //else // the search was completed //{ //} }