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:
- Is it possible to make my project architecture work?
- Is it not worth changing the architecture?
- 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.
RunWorkerCompleted
andPropgessChanged
if there is one - Gardes