0
代碼的主要形式有:?爲什麼進度條的值保持不變,當讀取的RegistryKey
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沒有阻塞,並有空閒時間來執行進度條。我對此感到困惑,我只是不使用狀態'塊'或其他東西。所以這一定是一些還有其他問題,還是你可以啓動我,並有一些理想? 謝謝你的時間。
非常感謝您,您是對的,但我已經使用Background Worker完成了我的jop。再次感謝,我明白了。 – user2826759