Sunday 30 November 2014

C# background worker example

1. XAML:

    
        



2. CS:\
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.ComponentModel;
using System.Security.AccessControl;
 
 
namespace PC_HELP
{
    /// 
    /// Interaction logic for FixFolders.xaml
    /// 
    public partial class FixFolders : Window
    {
        BackgroundWorker m_oWorker;
 
        
 
        public FixFolders()
        {
            InitializeComponent();
            m_oWorker = new BackgroundWorker();
 
            // Create a background worker thread that ReportsProgress &
            // SupportsCancellation
            // Hook up the appropriate events.
            m_oWorker.DoWork += new DoWorkEventHandler(m_oWorker_DoWork);
            m_oWorker.ProgressChanged += new ProgressChangedEventHandler
                    (m_oWorker_ProgressChanged);
            m_oWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                    (m_oWorker_RunWorkerCompleted);
            m_oWorker.WorkerReportsProgress = true;
            m_oWorker.WorkerSupportsCancellation = true;
        }
 
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            m_oWorker.RunWorkerAsync();
        }
        void m_oWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        { }
        void m_oWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
 
            // This function fires on the UI thread so it's safe to edit
 
            // the UI control directly, no funny business with Control.Invoke :)
 
            // Update the progressBar with the integer supplied to us from the
 
            // ReportProgress() function.  
 
            PG1.Value = e.ProgressPercentage;
            lblStatus.Text = "Processing......" + PG1.Value.ToString() + "%";
        }
        void m_oWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // The sender is the BackgroundWorker object we need it to
            // report progress and check for cancellation.
            //NOTE : Never play with the UI thread here...
 
            string[] folders = System.IO.Directory.GetDirectories(@"", "*", System.IO.SearchOption.TopDirectoryOnly);
 
            StringBuilder sb = new StringBuilder();
            MessageBox.Show(folders[5].Substring(18));
            for (int i = 0; i <= folders.Length - 1; i++)
            {
                BSCLib.AddDirectorySecurity(folders[i], folders[i].Substring(18), FileSystemRights.FullControl);
                m_oWorker.ReportProgress(i*100/folders.Length);
                    if (m_oWorker.CancellationPending)
                {
                    // Set the e.Cancel flag so that the WorkerCompleted event
                    // knows that the process was cancelled.
                    e.Cancel = true;
                    m_oWorker.ReportProgress(0);
                    return;
                }
            }
 
            //Report 100% completion on operation completed
            m_oWorker.ReportProgress(100);
        }
 
 
 
    }
}

Reference:

http://www.codeproject.com/Articles/99143/BackgroundWorker-Class-Sample-for-Beginners

No comments:

Post a Comment