Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-21
2
Medium Priority
?
1,637 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 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

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…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

688 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