c #WackgroundWorker和Treeview在winform中

时间:2015-04-14 17:48:19

标签: c# .net winforms treeview

环境:C#,VStudio 2013,4.5 Framework,Winforms

目标:在树视图中插入文件夹(子文件夹+文件)的内容,以便用户可以选择所需的内容。显示一个进度条,显示在TreeView中加载文件和文件夹的进度。

到目前为止我做了什么:我的目标中的一切都是......

错误:" 此BackgroundWorker当前正忙,无法同时运行多个任务"。当我在应用程序运行时使用其他应用程序时,有时会出现此错误。

我的代码

     void backgroundWorkerTreeView_DoWork(object sender, DoWorkEventArgs e)
                {
                    var progress = (((float)(int)e.Argument / (float)totalFilesInTN) * 100);
                    var value = (int)Math.Round((float)progress);
                    backgroundWorkerTreeView.ReportProgress(value, e.Argument.ToString());
                }
void backgroundWorkerTreeView_ProgressChanged(object sender, ProgressChangedEventArgs e)
                {
                    stStripBarMain.Value = e.ProgressPercentage;
                    toolStripStatusLabelPrct.Text = " Loading " + e.UserState + " of " + totalFilesInTN;
                }
void backgroundWorkerTreeView_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
                {
                    //do the code when bgv completes its work
                }

        private void ListDirectory(TreeView treeView, string path)
                {
                    try
                    {
                        treeView.Nodes.Clear();
                        if (path != "")
                        {
                            var rootDirectoryInfo = new DirectoryInfo(path);
                            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo, 0));
                        }
                    }
                    catch (Exception e)
                    {
                        txtLog.Text = txtLog.Text + "[" + DateTime.Now + "] " + e.Message + "\r\n";
                    }
                }

    private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo, int indice)
            {

                    var directoryNode = new TreeNode(directoryInfo.Name);
                    foreach (var directory in directoryInfo.GetDirectories())
                    {
                        if ((directory.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
                        {
                            directoryNode.Nodes.Add(CreateDirectoryNode(directory, indice));
                        }
                    }
                    foreach (var file in directoryInfo.GetFiles())
                    {
                        if ((IsNullOrEmpty(ext)) ||
                            (Array.IndexOf(ext, Path.GetExtension(file.Name.ToLowerInvariant())) >= 0))
                        {
                            if ((GetTriggerEvent(file.FullName).Contains(txtParamEvent.Text)) || (txtParamEvent.Text == ""))
                            {
                                indice++;
                                backgroundWorkerTreeView.RunWorkerAsync(indice);
                                TreeNode newTN = new TreeNode();
                                newTN.Text = file.Name + @"   (" + GetTriggerEvent(file.FullName) + @")";
                                newTN.Name = file.FullName;
                                directoryNode.Nodes.Add(newTN);
                                newTN.Tag = "msg";
                            }
                        }
                        Application.DoEvents();
                    }
                    return directoryNode;
            }

感谢您的帮助

理查德

0 个答案:

没有答案