Solved

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

Posted on 2014-01-21
2
1,539 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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