メインフォームのコード:
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 ステートメントを使用して、メイン フォームのレジストリ キー値を再帰的に読み取りました。プログレス バー フォームを実行するために新しいスレッドを開始しました。現在の問題は、アプリの実行時にプログレス バー フォームが表示されないことです。これは、メイン フォームが完了したときに表示されます (ただし、プログレス バーの値は同じままである、または増加しないと言う)。メインジョブがブロックされておらず、プログレスバーを実行する自由な時間があるかどうかを確認できるかどうかについて誰かが言いました.私はこれについて混乱しており、状態「ブロック」などを使用していません.質問、または私に何かを立ち上げて、理想を教えてもらえますか? 御時間ありがとうございます。