Solved

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

Posted on 2014-01-21
2
1,498 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 450 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 50 total points
ID: 39799749
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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