Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
Updated 3-Oct-12 0:31am
v2
Comments
Kenneth Haugland 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 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.

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:

C#
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)));
            }
        }
    }
}
 
Share this answer
 
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
C#
// This sleep will allow the UI thread to refresh the display.
totalCounter++;
if (totalCounter % 100 == 0)
    System.Threading.Thread.Sleep(80);
 
Share this answer
 
v2
Comments
Dave Kreskowiak 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900