1. XAML:

 

<Window x:Class="PC_HELP.FixFolders"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FixFolders" Height="364" Width="550">
    <Grid>
        <Button Content="Set Student Home Folder Access Right" HorizontalAlignment="Left" Margin="123,25,0,0" VerticalAlignment="Top" Width="265" Height="57" Click="Button_Click"/>
        <ProgressBar x:Name="PG1" HorizontalAlignment="Left" Height="51" Margin="83,158,0,0" VerticalAlignment="Top" Width="386"/>
        <TextBox x:Name="lblStatus" HorizontalAlignment="Left" Height="23" Margin="182,109,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>

    </Grid>
</Window>

 

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
{
    /// <summary>
    /// Interaction logic for FixFolders.xaml
    /// </summary>
    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(@"<directory>", "*", 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

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today48
Yesterday83
This week672
This month1968
Total416328

Visitor Info

  • IP: 54.227.51.103
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login