I have a winform app that reads in txt files and loads the contents to a datagrid. This process could take 1 to 30+ seconds depending on size of file. When large files are used the entire winform UI gets locked up. I guess I need to move the process to another thread and display a progress bar. I have been reading about the System.Threading and backgroundworker class however, most of those examples online have a for loop with a counter for demostations. Looking for a real example?
Here is sample code...looking for help to add in threading
//open file button click event
private void btnOpenFile_Click(object sender, EventArgs e)
Cursor.Current = Cursors.WaitCursor;
//show dialog, load xml to grid
OpenFile(true, true, false);
Cursor.Current = Cursors.Default;
private void OpenFile(bool openDialog, bool loadGrid, bool selectXls)
//method that could take up to 30 seconds to complete
//where should thread go and how to update progress bar
//get data from file and then bind to gridview
private void LoadDataToGrid(string fileName)
myDataTable = new DataTable();
//this method take the most time
myDataTable = myObj.PopulateDataTable(fileName, myDataTable);
//bind to datagrid view
//this line can take 1-5 seconds when the PopulateDataTable method takes a long time
dgv.DataSource = myDataTable;
// Initialize basic DataGridView properties.
PopulateDataTable is method in a seperate class file \ dll
Where should the threading be handled and how does the progress bar get updated if processing is handled in seperate dll \ class file?
Thanks in advance and happy new year!