Example #1
0
        /// <summary>
        /// 4线程查找并把结果放入控件的异步方法
        /// </summary>
        /// <param name="lb">listbox控件</param>
        /// <param name="path">查找的起始路径</param>
        /// <param name="ct">用于安全结束线程的对象</param>
        /// <param name="pattern">查找的文件或目录名</param>
        /// <returns></returns>
        public static async Task AddLBItemByThreads(ListBox lb, string path, CancellationToken ct, string pattern)
        {
            Task t = new Task(() =>
            {
                var dirInfo = new DirectoryInfo(path);
                var _files  = dirInfo.GetFiles(pattern);
                var _dirs   = dirInfo.GetDirectories();
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern.Replace("*", ".*"));

                //Thread.Sleep(2000);
                MyItemManager.UpdateByMainThread(lb, _files);
                Queue <DirectoryInfo> quedir = new Queue <DirectoryInfo>();
                try {
                    foreach (var i in _dirs)
                    {
                        quedir.Enqueue(i);
                        if (reg.IsMatch(i.Name))
                        {
                            MyItemManager.UpdateByMainThread(lb, i);
                        }
                        ct.ThrowIfCancellationRequested();
                    }
                    int j = 2;
                    while (j > 0 && quedir.Count != 0)
                    {
                        var p   = quedir.Dequeue();
                        Task tt = AddLBItemAnsyc(lb, p.FullName, ct, pattern);
                    }
                    while (quedir.Count != 0)
                    {
                        //Thread.Sleep(2000);
                        ct.ThrowIfCancellationRequested();
                        var dir   = quedir.Dequeue();
                        var files = dir.GetFiles(pattern);
                        foreach (var ii in _dirs)
                        {
                            if (reg.IsMatch(ii.Name))
                            {
                                MyItemManager.UpdateByMainThread(lb, ii);
                            }
                            ct.ThrowIfCancellationRequested();
                        }
                        MyItemManager.UpdateByMainThread(lb, files);
                    }
                }
                catch (Exception) {
                }
            });

            t.Start();
            await t;
        }
Example #2
0
        /// <summary>
        /// 查找并把结果放入控件的普通方法
        /// </summary>
        /// <param name="lb"></param>
        /// <param name="path"></param>
        /// <param name="ct"></param>
        /// <param name="pattern"></param>
        public static void AddLBItem(ListBox lb, string path, CancellationToken ct, string pattern)
        {
            var dirInfo = new DirectoryInfo(path);
            var _files  = dirInfo.GetFiles(pattern);
            var _dirs   = dirInfo.GetDirectories();

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(pattern.Replace("*", ".*"));

            Queue <DirectoryInfo> quedir = new Queue <DirectoryInfo>();

            foreach (var i in _dirs)
            {
                quedir.Enqueue(i);
                if (reg.IsMatch(i.Name))
                {
                    MyItemManager.UpdateByMainThread(lb, i);
                }
                ct.ThrowIfCancellationRequested();
            }
            //Thread.Sleep(2000);
            MyItemManager.UpdateByMainThread(lb, _files);

            while (quedir.Count != 0)
            {
                //Thread.Sleep(2000);
                ct.ThrowIfCancellationRequested();
                var dir   = quedir.Dequeue();
                var dirs  = dir.GetDirectories();
                var files = dir.GetFiles(pattern);
                foreach (var i in dirs)
                {
                    if (reg.IsMatch(i.Name))
                    {
                        MyItemManager.UpdateByMainThread(lb, i);
                    }
                    ct.ThrowIfCancellationRequested();
                }
                MyItemManager.UpdateByMainThread(lb, files);
            }
        }