首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教,为何小弟我的进度条不能显示?(读取注册表)

2013-10-04 
请问,为何我的进度条不能显示?(读取注册表).Code in the main form:private delegate bool IncreaseProbar

请问,为何我的进度条不能显示?(读取注册表).
Code in the main form:

private delegate bool IncreaseProbarHandler(int nIncVal);   //Declare a delegate to increase the progress bar value.
        private IncreaseProbarHandler _IncHanler = null;
        private List<Microsoft.Win32.RegistryKey> _RKeys = new List<Microsoft.Win32.RegistryKey>(); //Store the RegistryKey.
        public MainForm() {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e) {
            new Thread(ProThread).Start();
            RecursiveRegedit(Microsoft.Win32.Registry.CurrentUser);
            //RecursiveRegedit(Microsoft.Win32.Registry.LocalMachine);
            MessageBox.Show("Done!");
        }
        //Recursive scan the registry.
        void RecursiveRegedit(Microsoft.Win32.RegistryKey regBoot) {
            if(regBoot == null) throw new ArgumentNullException("Null Item!");
            string[] vals = regBoot.GetValueNames();
            foreach(var v in vals) {
                if(regBoot.GetValue(v) != null) {
                    string s = regBoot.GetValue(v).ToString();
                    if(s.StartsWith("C:", StringComparison.CurrentCultureIgnoreCase))
                        _RKeys.Add(regBoot);    //Add to 'List'.
                }
            }
            if(regBoot.SubKeyCount <= 0)//Exit.
                return;
            else {//Recursive.
                string[] subs = regBoot.GetSubKeyNames();
                foreach(string s in subs) {
                    try {//Try...catch the not accessible notes exception.
                        RecursiveRegedit(regBoot.OpenSubKey(s, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl));
                    }
                    catch {
                    }
                }
            }
            regBoot.Close();    //Close.
        }
        /// <summary>
        /// Show Progress bar form.
        /// </summary>
        void ShowProbar() {
            ProgressBarForm proForm = new ProgressBarForm();
            _IncHanler = new IncreaseProbarHandler(proForm.IncreaseProbarVal);
            proForm.Show();
        }
        /// <summary>
        /// Sub Thread to perform the progress bar.


        /// </summary>
        void ProThread() {
            MethodInvoker mInvoker = new MethodInvoker(ShowProbar);
            this.BeginInvoke(mInvoker);
            Thread.Sleep(1000);
            bool incResult = false; //The status each time when trying to increase the progress bar value.
            do {
                Thread.Sleep(5);
                incResult = (bool)this.Invoke(this._IncHanler, new object[] { 2 });
            } while(incResult);
        }



Code in the Progress bar form:
/// <summary>
        /// Increase the value of the progress bar.
        /// </summary>
        /// <param name="incVal">The value to increase.</param>
        /// <returns>True if increase successful,otherwise false.</returns>
        public bool IncreaseProbarVal(int incVal) {
            if(incVal <= 0) throw new ArgumentOutOfRangeException("Increase value can't the a negative.");
            if(proBar.Value + incVal < proBar.Maximum) {
                proBar.Value += incVal;
                return true;
            }
            else {
                proBar.Value = proBar.Maximum;
                return false;
            }
        }


Description:
描述一下, 我的操作是,递归读取注册表中的信息.我想增加一个显示进度条的窗口,所以我通过异步的方式(多线程),即用委托的方式显示,但是不懂为什么进度条窗口的 进度值不变,主窗口结束后才显示(但是进度条还是没有变化).
多线程好难调试哈,而且切换为单线程又不怎么合乎逻辑,所以...也很难调试...
[解决办法]
你能否确定方法 ProThread() 是在子线程中执行?

热点排行