0

主窗体中的代码:

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

进度条表单中的代码:

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

描述:我使用try catch语句递归读取主窗体中的注册表键值。我启动了一个新线程来执行进度条窗体。目前的问题是,运行应用程序时没有出现进度条窗体。它显示主窗体何时完成(但进度条的值保持不变,或者说不增加)。有人说我是否可以确定主要 jop 没有被阻塞并且有空闲时间来执行进度条。我对此感到困惑,我只是不使用状态“阻塞”或其他东西。所以那一定是其他的问题,或者你可以让我做一些事情并有一些理想吗?谢谢你的时间。

4

1 回答 1

0

真是一团糟……很多不必要的线程和调用()。=\

“当前的问题是,运行应用程序时没有出现进度条表单。它显示主表单何时完成”

您对 RecursiveRegedit() 的调用正在主线程 UI 上运行......因此在递归调用完成之前无法更新任何内容。

您需要像使用 ProThread() 一样从另一个线程运行 RecursiveRegedit()。

于 2013-10-04T03:45:47.123 回答