Search code examples
wpfbackgroundworkermessagebox

WPF BackgroundWorker process RunWorkerCompleted is called multiple times, why?


I have some code that starts a background process for search in my WPF application:

 private void RunSearch(SearchArguments arguments)
    {
        _getSearchResults.DoWork += GetSarchFromDb;
        _getSearchResults.RunWorkerCompleted += SearchFinished;
        _getSearchResults.RunWorkerAsync(arguments);
    }

RunSearch is exicuted from button_click event.

I have a messagebox inside my SearchFinished method that shows "No Results Found". For some reason, the SearchFinished method is called multiple times sometimes, which causes multiple MessageBoxes to be shown. Is there a workaround for this?


Solution

  • Rushed the gun with posting this a bit.

    Since I'm wiring up the events on button click, every time I click the button, the number of times the event executes goes up. I moved the wire up to the constructor, and my problem was fixed.