Focus Stealing with a Thread

I am using a thread to make calculations and update a listbox on a Window. I am using the thread because the calculations may take several hours, and I want the user to be able to use other portions of the software while these calculations are running. The problem is every time the listbox is updated, the focus is moved to the window with the listbox. Is there any way to prevent this focus theft?

That shouldn’t happen by default, check your code for the use of Show, to see if you’re showing the window on every listbox update. Even if its shown already this will force it to take focus and come to the front of your apps open windows.

Write results in an array or in-memory database. If the thread is finished, populate the results in one step into the listbox.

A trick to find out what’s happening:

  • in the other window (or control) that is losing focus, add the LostFocus event
  • put a breakpoint there
  • run your app in the debugger, and when the focus is lost, it will hit the breakpoint
  • in the debugger, switch to each thread, and you will see what is happening to cause the focus switch.

My guess is you have a Window.Show() call you forgot about.

Thanks to all of you. It was such a simple fix. A stray show statement caused all the trouble. I am so used to focus stealing in Windows, that I assumed the fix was related to the operating system.