I have two lists, one is a master, the other is a child. When the index changes on the main, the child list is populated accordingly with entries related to the main. My problem arises when one wizard takes a long time to get all the records, and before he finishes receiving the records, the user clicks on another wizard, which takes less time to complete. What happens is that in the end, the wizard, which took more time, fills in the window for the list of children, even if the user is no longer on this host.
I use BackgroundWorker threads to populate.
bw_LoadAll.DoWork += new DoWorkEventHandler(bg_LoadAllWork); bw_LoadAll.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_LoadAllWorkCompleted); bw_LoadAll.WorkerSupportsCancellation = true;
I subscribed to the SelectedIndexChanged event for the wizard, and I set the canceled to true:
bw_LoadAll.CancelAsync();
Here is the code in the DoWork method:
List<object> s = Repository.Instance().LoadAll(); if (!bw_LoadAll.CancellationPending) { e.Result = s; } else { e.Cancel = true; }
But for some reason, the code for the filled employee continues to be called. Here is the working code:
if (!e.Cancelled) { ddl.DataSource = e.Result; ddl.DisplayMember = "QuickName"; ddl.ValueMember = "ID"; }
Is there anything else I have to do to cancel this thread from returning?
source share