There is a file search method:

static public void FindLibrarys(String directoriesPath) { ... //Ищет файлы. Каждый найденный файл передаёт в метод. ProgramsList.FormationProgramsList(file); } 

This method processes the file and places in its ObservableCollection:

 static public void FormationProgramsList(String file) { ... // NewData возвращает новый экземпляр структуры. _fileList.Add( NewData( fileName + " | x" + bitCapacityProgram + " | " + myFileInfo.DirectoryName + fileVersion + productName, _fileList.Count, bitCapacityProgram == KompasCapacity ) ); } 

In the ViewModel, this collection is associated with the model collection:

 public ProjectViewModel() { InitializeComponent(); _model = (ProjectModel)this.Resources["Model"]; backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker")); _model.ProgramsList = ProgramsList.FileList; // Ну и с самим контроллом ResultName.ItemsSource = _model.ProgramsList; } 

So I had to do this search by button, but that the interface does not hang. I did it through Application.DoEvent, but this is Windows.Forms. By this I was told to try by other means. I decided to try through BackgroundWorker.

In VM, in the handler of the button I call:

 backgroundWorker.RunWorkerAsync(); 

And wrote the following code:

 private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { foreach (String path in _model.SearchPathsList) { if (_isContinueSearch) { LibraryFinder.FindLibrarys(path); } else break; } } 

Everything starts, only at the first found file throws an exception: System.NotSupportedException when adding an item to the _fileList collection in the FormationProgramsList method.

Questions:

  1. Is it possible to make my project architecture work?
  2. Is it not worth changing the architecture?
  3. Are there any more convenient tools that could be used instead of BackgroundWorker?

Changes:

 // Здесь я просто возвращаю кнопку в исходное состояние, т.к. при первом нажатии она меняет параметры и становится кнопкой остановки поиска. private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // Возврат параметров поиска в исходное состояние. FindApp.Tag = true; FindApp.Content = "Найти приложения"; } 

And there is no ProgressChanged.

  • Show RunWorkerCompleted and PropgessChanged if there is one - Gardes
  • Do you want the files to be added to the list as they are found or all at once? - Gardes
  • Yes, I just need to be added one by one. - Sergey

1 answer 1

In this case, add the ProgressChanged event handler and set the WorkerReportsProgress="True" property (the same in xaml, where the 'DoWork` handler was added).

 private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { string file = (string)e.UserState; ProgramsList.FormationProgramsList(file); } private void FindLibrarys(String directoriesPath) { //Ищет файлы. Каждый найденный файл передаёт в метод. backgroundWorker.ReportProgress(0, file); } 

Also, make the methods non-static, otherwise you won't have access to backgroundWorker or pass the reference to backgroundWorker to the FindLibrarys method.

  • Many thanks, I will try. I will report on the results. - Sergey
  • Everything turned out, thanks again. - Sergey
  • @ Sergey, glad to help!) - Gardes