Skip to main content
added 259 characters in body; added 13 characters in body
Source Link
Heinzi
  • 173.3k
  • 61
  • 386
  • 554

You are just creating these objects in the background; that doesn't really make a difference. You need to call your methods in the background:

 public YourTextBox_TextChanged(...) { var bw = new BackgroundWorker(); bw.DoWork += (sender, args) => { // do your lengthy stuff here -- this will happen in a separate thread Manager.SetItemText(e.Value) } bw.RunWorkerCompleted += (sender, args) => { if (args.Error != null) // if an exception occurred during DoWork, MessageBox.Show(args.Error.ToString()); // do your error handling here // Do whatever you want to do after the SetItemText has completed. // We are back in the UI thread here. ... } bw.RunWorkerAsync(); // start the background worker } 

PS: Make sure your Manager.SetItemText method is thread-safe! Using background threads, it is quite possible that multiple instances of Manager.SetItemText run in parallel (if a second TextChanged event arrives before the first SetItemText has been completed).

You are just creating these objects in the background; that doesn't really make a difference. You need to call your methods in the background:

 public YourTextBox_TextChanged(...) { var bw = new BackgroundWorker(); bw.DoWork += (sender, args) => { // do your lengthy stuff here -- this will happen in a separate thread Manager.SetItemText(e.Value) } bw.RunWorkerCompleted += (sender, args) => { if (args.Error != null) // if an exception occurred during DoWork, MessageBox.Show(args.Error.ToString()); // do your error handling here // Do whatever you want to do after the SetItemText has completed. // We are back in the UI thread here. ... } bw.RunWorkerAsync(); // start the background worker } 

You are just creating these objects in the background; that doesn't really make a difference. You need to call your methods in the background:

 public YourTextBox_TextChanged(...) { var bw = new BackgroundWorker(); bw.DoWork += (sender, args) => { // do your lengthy stuff here -- this will happen in a separate thread Manager.SetItemText(e.Value) } bw.RunWorkerCompleted += (sender, args) => { if (args.Error != null) // if an exception occurred during DoWork, MessageBox.Show(args.Error.ToString()); // do your error handling here // Do whatever you want to do after the SetItemText has completed. // We are back in the UI thread here. ... } bw.RunWorkerAsync(); // start the background worker } 

PS: Make sure your Manager.SetItemText method is thread-safe! Using background threads, it is quite possible that multiple instances of Manager.SetItemText run in parallel (if a second TextChanged event arrives before the first SetItemText has been completed).

Source Link
Heinzi
  • 173.3k
  • 61
  • 386
  • 554

You are just creating these objects in the background; that doesn't really make a difference. You need to call your methods in the background:

 public YourTextBox_TextChanged(...) { var bw = new BackgroundWorker(); bw.DoWork += (sender, args) => { // do your lengthy stuff here -- this will happen in a separate thread Manager.SetItemText(e.Value) } bw.RunWorkerCompleted += (sender, args) => { if (args.Error != null) // if an exception occurred during DoWork, MessageBox.Show(args.Error.ToString()); // do your error handling here // Do whatever you want to do after the SetItemText has completed. // We are back in the UI thread here. ... } bw.RunWorkerAsync(); // start the background worker }