Solved

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

Posted on 2014-01-21
2
1,439 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 69

Assisted Solution

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now