I have an app that I want to run the updating of each datagridview in the background on load concurrently instead of running sequentially on load. The code that gets loaded on app load is.
//##############################Movies
//update the list of filepaths
movieFilePaths_ = manageFilePathsFile.ReadFilePathsTextFile("Movie", filename);
//populate the list box
manageFilePathsFile.PopulateListBoxWithFilePaths(listBoxMoviePaths, movieFilePaths_);
if (movieFilePaths_.Count > 0)
{
movieList_ = onAppInitialLoad.GetMovieFilesFromActivePaths(movieFilePaths_, movieList_);
}
movieList_ = movieList_.OrderBy(x => x.FileName).ToList();
dataGridView1.DataSource = movieList_;
//##############################MusicVidoes
//update the list of filepaths
musicVideoFilePaths_ = manageFilePathsFile.ReadFilePathsTextFile("MusicVideo", filename);
//populate the list box
manageFilePathsFile.PopulateListBoxWithFilePaths(listBoxMusicVideoPaths, musicVideoFilePaths_);
if (musicVideoFilePaths_.Count > 0)
{
musicVideoList_ = onAppInitialLoad.GetMusicVideoFilesFromActivePaths(musicVideoFilePaths_, musicVideoList_);
}
musicVideoList_ = musicVideoList_.OrderBy(x => x.BandName).ThenBy(x => x.FileName).ToList();
dataGridViewMusicVideo.DataSource = musicVideoList_;
//##############################Rugby
////update the list of filepaths
rugbyGameFilePaths_ = manageFilePathsFile.ReadFilePathsTextFile("Rugby", filename);
//populate the list box
manageFilePathsFile.PopulateListBoxWithFilePaths(listBoxRugbyFilePaths, rugbyGameFilePaths_);
if (rugbyGameFilePaths_.Count > 0)
{
rugbyGameList_ = onAppInitialLoad.GetRugbyGamesFilesFromActicePaths(rugbyGameFilePaths_, rugbyGameList_);
}
dataGridViewRugby.DataSource = rugbyGameList_;
interfaceOutputs.PopulateInterfaceOutputs(dataGridViewRugby, labelDataGridRugby);
Essentially each block beginning with //######################{name} would be run currently with the block above. I have implemented the backgroundworker on button presses to add and remove paths and running a single instance is easy.
I am stumped though on the best way to implement mutliple threads with out duplicating the current way I have of doing it, which is the code in full each time.
This is what I do to update a path and get the files from the background while the UI is still functioning.
Should I look at implementing backgroundworker as a class or method and calling that with parameters which would be each block of code? although I don't quite get that.
BackgroundWorker bw = new BackgroundWorker();
// this allows our worker to report progress during work
bw.WorkerReportsProgress = true;
// what to do in the background thread
bw.DoWork += new DoWorkEventHandler(
delegate(object o, DoWorkEventArgs args)
{
BackgroundWorker b = o as BackgroundWorker;
String folderPath = this.folderBrowserDialog1.SelectedPath;
string prefix = manageFilePathsFile.GetPrefixForFilePathTextFile(tabControlName);
string fulltextFileLine = prefix + " - " + folderPath;
bool found = movieFilePaths_.Any(c => c == fulltextFileLine);
if (!found)
{
//temp list to hold the 1 path to send to the function
List<string> addOnePath_ = new List<string>();
addOnePath_.Add(folderPath);
//add it to the master list of filepaths also
movieFilePaths_.Add(fulltextFileLine);
manageFilePathsFile.AddToFilePathTextFile(FILEPATH, folderPath, prefix);
// this allows another thread to call a item that was created / being managed by another thread
this.Invoke(new MethodInvoker(delegate()
{
manageFilePathsFile.PopulateListBoxWithFilePaths(listBoxMoviePaths, movieFilePaths_);
}));
//get the movies from that path
List<Movie> tempMovieList_ = new List<Movie>();
tempMovieList_ = movie.GetMoviesFromNewPath(folderPath);
//adds the 2 lists together
movieList_.AddRange(tempMovieList_);
//http://tech.pro/tutorial/776/csharp-tutorial-binding-a-datagridview-to-a-collection
//http://stackoverflow.com/questions/9758577/c-sharp-datagridview-not-updated-when-datasource-is-changed
tempMovieList_.Clear();
}
}
);// bw.DoWork += new DoWorkEventHandler(delegate(object o, DoWorkEventArgs args
// what to do when progress changed (update the progress bar for example)
bw.ProgressChanged += new ProgressChangedEventHandler(
delegate(object o, ProgressChangedEventArgs args)
{
});
//bw1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
// bw.ProgressChanged += delegate { ... };
// what to do when worker completes its task (notify the user)
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
delegate(object o, RunWorkerCompletedEventArgs args)
{
//reset the data grid view
movieList_ = movieList_.OrderBy(x => x.FileName).ToList();
dataGridView1.DataSource = null;
dataGridView1.DataSource = movieList_;
//update the Interface outputs
// eg Movie Count: {count}
interfaceOutputs.PopulateInterfaceOutputs(dataGridView1, labelDataGridMovies);
});
bw.RunWorkerAsync();
// this allows our worker to report progress during work
bw.WorkerReportsProgress = true;