0

I'm making this simple program in WPF (not Windows Forms), it's for an assignment at university. The program has two buttons and a listbox. When the program starts, the first button is enabled while the second one is disabled. When the user clicks the first button, a dialog box opens up that has an "OK" button on it. When the "OK" button is clicked, it gets all the file names from a directory and displays it on a listbox. But this is done using a background worker so the listbox adds the files gradually as the files are found. Now what I want to do is that AFTER the background worker has completed its work, that is, listed all the files in the directory, I want to make the second button enabled that was previously disabled. I know the syntax for making buttons enabled or disabled, you set true or false to .IsEnabled property. For example:

    //this will disable the button
    buttonName.IsEnabled = false; 

But what I want to know is how and where do I use this property (what additional code do I need) so that the second button becomes enabled only AFTER the background worker has fully completed its job. With my present code, what happens is that the second button becomes enabled as soon as the "OK" button is clicked and as as soon as the background worker starts. In the code below, "btnSort" is the name of the second button that should be enabled after the background worker is done. You could probably ignore the DirSearch method at the end, it's just a method for getting files from a directory. I know there are other ways to search directories but I was told to use this method.

    public partial class MainWindow : Window
    {
        BackgroundWorker backgroundWorker;

        string sourcePath = "";

        List<string> list1 = new List<string>();

        public MainWindow()
        {
            InitializeComponent();

            backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerReportsProgress = true;
            //take this out if cancel not used
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.DoWork +=
                new DoWorkEventHandler(backgroundWorker_DoWork);
            backgroundWorker.ProgressChanged +=
                new ProgressChangedEventHandler(backgroundWorker_ProgressChanged);
        }    


        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
                folderDialog.SelectedPath = @"C:\temp";
                System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();

                if (result.ToString() == "OK")
                {
                    if (listBox1.Items.Count != 0)
                    {
                        listBox1.Items.Clear();
                    }

                    if (list1.Count != 0)
                    {
                        list1.Clear();
                    }

                    sourcePath = folderDialog.SelectedPath;

                    backgroundWorker.RunWorkerAsync(sourcePath);

                    if (btnSort.IsEnabled == false)
                    {
                        btnSort.IsEnabled = true;
                    }                
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }    
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DirSearch(e.Argument.ToString());
            MessageBox.Show("Complete!");
        }

        private void backgroundWorker_ProgressChanged(object sender,
        ProgressChangedEventArgs e)
        {
            FileFetchState state = (FileFetchState)e.UserState;
            listBox1.Items.Add(state.FetchedFile);

        }

        public class FileFetchState
        {
            public string FetchedFile
            {
                get;
                set;
            }

            public FileFetchState(string fetchedFile)
            {
                FetchedFile = fetchedFile;
            }
        }

        public void DirSearch(string sourcePath)
        {
            try
            {
                foreach (string f in Directory.GetFiles(sourcePath))
                {
                    string fileName = System.IO.Path.GetFileName(f);

                    if (!listBox1.Items.Contains(fileName))
                    {
                        //code for adding to list1
                        list1.Add(fileName);

                        backgroundWorker.ReportProgress(0,
                            new FileFetchState(fileName));

                        System.Threading.Thread.Sleep(1);
                    }
                }
                foreach (string d in Directory.GetDirectories(sourcePath))
                {
                    DirSearch(d);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
dbc
  • 104,963
  • 20
  • 228
  • 340
Steve Way
  • 167
  • 2
  • 3
  • 11
  • nevermind guys. I figured it out. just gotta use BackgroundWorker.RunWorkerCompleted – Steve Way Mar 19 '13 at 02:21
  • Related or duplicate: [How to use WPF Background Worker](https://stackoverflow.com/q/5483565/3744182). – dbc Apr 24 '21 at 16:58
  • You can use this if anyone is interested http://msdn.microsoft.com/en-AU/library/system.componentmodel.backgroundworker.runworkercompleted.aspx – Steve Way Mar 19 '13 at 02:27

0 Answers0