Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# .NET 4.5 - Updating Winforms GUI from background threads

Posted on 2014-01-21
2
Medium Priority
?
1,677 Views
Last Modified: 2014-02-04
Hey all,

Using .Net 4.5, I want to update a datagrid control to display the contents of multiple CSV files. Each CSV file will be one line of the datagrid.

I am confident in reading and processing the CSV files, but I don't know how to do this in a background thread that can update the datagrid on the GUI.

What is the best way to do this in the latest .net framework?

Any guidance appreciated. Thankyou.
0
Comment
Question by:mhdi
2 Comments
 
LVL 17

Accepted Solution

by:
Barry Cunney earned 1800 total points
ID: 39799613
Hi mhdi,
There is a BackgroundWorker Class in namespace System.ComponentModel - .Net 4.5

The following is not full code, but will give you an idea of how the above can potentially be used in your situation
 
// Instantiate new background worker object
BackgroundWorker bw_Display_File_Content = new BackgroundWorker();


// Specify that the background worker can be cancelled and also can report progress
bw_Display_File_Content.WorkerSupportsCancellation = true;
bw_Display_File_Content.WorkerReportsProgress = true;



// Do the actual work of getting file content
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    string[] files = Directory.GetFiles(txtFolderPath.Text, "*.csv");
    int row = 0;

    // Loop through each file
    foreach (string file in files)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform the work and report progress.
           
            // Read file and add to data grid
            using (StreamReader sr = new StreamReader("FilePath"))
            {
                

                 string[] file_content = sr.ReadToEnd()
                 dataGridView1.Rows.Add(); 
                 dataGridView1[0, row].Value = file_content

               
                    
             }
            
	   
            worker.ReportProgress((i * 10));
        
           
            }
       

           row++;

       }
}



private void buttonStart_Click(object sender, RoutedEventArgs e)
{
    if (bw_Display_File_Content.IsBusy != true)
    {
        bw_Display_File_Content.RunWorkerAsync();
    }
}



private void w_Display_File_Content_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}

Open in new window

0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 39799749
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question