后台工作程序完成后启用按钮(C#)

时间:2013-03-19 01:36:45

标签: button background worker complete

我在WPF(不是Windows Forms)中创建这个简单的程序,它是在大学里完成的。该程序有两个按钮和一个列表框。程序启动时,第一个按钮启用,第二个按钮禁用。当用户单击第一个按钮时,会打开一个对话框,其上有一个“确定”按钮。单击“确定”按钮后,它将从目录中获取所有文件名,并将其显示在列表框中。但这是使用后台工作程序完成的,因此列表框会在找到文件时逐渐添加文件。现在我想要做的是,在后台工作人员完成其工作之后,即列出目录中的所有文件,我想启用之前禁用的第二个按钮。我知道启用或禁用按钮的语法,您将true或false设置为.IsEnabled属性。例如:

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

但我想知道的是我如何以及在何处使用此属性(我需要哪些附加代码),以便第二个按钮仅在后台工作人员完全完成其工作后才启用。使用我现在的代码,只要单击“确定”按钮并且后台工作程序启动后,第二个按钮就会启用。在下面的代码中,“btnSort”是后台工作程序完成后应启用的第二个按钮的名称。你可能最后会忽略DirSearch方法,它只是一种从目录中获取文件的方法。我知道还有其他方法来搜索目录,但我被告知要使用这种方法。

    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);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)