troubleshooting Question

c# - show progress bar in winform app

Avatar of Jeff Fillegar
Jeff FillegarFlag for United States of America asked on
.NET ProgrammingC#
10 Comments3 Solutions6871 ViewsLast Modified:
Hello experts,

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
   LoadDataToGrid(fileName);
}
//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.
    InitializeDataGridView();
    ...
}

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!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 3 Answers and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros