Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi Everyone...
 
i want to show Temporary files in a datagrid , hence it is a long term process i use background worker in my C# .net WPF application .
 
my Code is
private System.ComponentModel.BackgroundWorker _background = new System.ComponentModel.BackgroundWorker();
 
   private void button1_Click(object sender, RoutedEventArgs e)
        {
          _background.RunWorkerAsync();
        }
 
    public MainWindow()
       {
           InitializeComponent();
           this._background.DoWork += new DoWorkEventHandler(_background_DoWork);
           this._background.RunWorkerCompleted += new       
           RunWorkerCompletedEventHandler(_background_RunWorkerCompleted);
           this._background.WorkerReportsProgress = true;
           _background.WorkerSupportsCancellation = true;
 
       }
 
void _background_DoWork(object sender, DoWorkEventArgs e)
        {
 
this.Dispatcher.Invoke((Action)(() =>
    {
        try
        {
            FileInfo[] files = new     
            DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
           
            foreach (FileInfo fi in files)
            {
                if (fi != null)              
                 {                 
                    dataGrid1.Items.Add(fi);           
                                         
                }
            }           
        }
        catch { }
    }));
}
 
void _background_RunWorkerCompleted(object sen, RunWorkerCompletedEventArgs e)
      {
 
          if (e.Cancelled)
          {
             MessageBox.Show("Cancelled");
          }
          else if (e.Error != null)
          {
               MessageBox.Show("Exception Thrown");
          }
 
     }
All the code is running but it hangs when datagrid is loading means my UI does not response when program is running .
 
What modification is needed to run background worker smoothly in the above condition ?
 
Thank You
Posted 3-Oct-12 1:29am
Edited 3-Oct-12 1:31am
v2
Comments
Kenneth Haugland at 3-Oct-12 6:35am
   
Why dont you add the files to a List or Collection and bind it to the Data Grid afterwords?
Ashraff Ali Wahab at 3-Oct-12 14:49pm
   
The code is not doing background work but constantly interacting with the UI by adding item to the datagrid.First add all the items to a collection like Kenneth said and then bind it to the datagrid in one shot.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Following your code, you completely wrecked the purpose of doing work on a background thread.
 
In the DoWork method, on a background thread, you told the UI thread (this.Dispatcher.Invoke) to execute the long running code, completely removing the point of the background thread.
 
The background thread should be Invoking a method to add the file to the datagrid.
 
I believe you're looking for something like this:
 
void _background_DoWork(object sender, DoWorkEventArgs e)
{
        FileInfo[] files = new     
        DirectoryInfo(System.IO.Path.GetTempPath()).GetFiles();
       
        foreach (FileInfo fi in files)
        {
            if (fi != null)              
            {                 
                this.Dispatcher.BeginInvoke((Action)(() => dataGrid1.Items.Add(fi)));
            }
        }
    }
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,
 
You should add to the background worker a short sleep in its main loop to allow the UI thread to refresh the display.
 
Try adding something like this to your foreach loop
// This sleep will allow the UI thread to refresh the display.
totalCounter++;
if (totalCounter % 100 == 0)
    System.Threading.Thread.Sleep(80);
  Permalink  
v2
Comments
Dave Kreskowiak at 8-Nov-12 11:43am
   
SAY WHAT?! "Put a thread sleep in the background worker thread so that the UI thread can refresh"? Ummmm, that makes no sense at all since the UI thread is not being blocked by the background worker thread at all.
 
I suggest you study up on threading under .NET. You apparently don't know how it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 8 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100