是BackgroundWorker
执行某个代码。完成后,它必须将变量写入label
. 但它不会发生。然后我决定把代码拆成BackgroundWorker
小块,执行完一段代码后,Console.WriteLine
显示段数( ),只有8个,前7个是条件检查,最后一个是输出。然后当我编译程序时,我在输出中看到:
1
2
3
4
5
6
7
它与什么有关?
工人代码:
private void RegistryChecker_DoWork(object sender, DoWorkEventArgs e)
{
int CanOptimisated = 0;
var printers = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RemoteComputer\NameSpace\{863aa9fd-42df-457b-8e4d-0de1b8015c60}");
//var onedrive = Registry.LocalMachine;
string unloaddlls = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer";
string superfetch = @"SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\PrefetchParameters";
string prioritycontrol = @"SYSTEM\CurrentControlSet\Control\PriorityControl";
string disableexecuting = @"SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management";
string enablesmartscreen = @"SOFTWARE\Policies\Microsoft\Windows\System";
Console.WriteLine(1);
try
{
printers.GetValue("");
CanOptimisated++;
guna2CheckBox1.Invoke(new Action(() =>
{
guna2CheckBox1.Enabled = true;
}));
}
catch
{
guna2CheckBox1.Invoke(new Action(() =>
{
guna2CheckBox1.Enabled = false;
}));
}
Console.WriteLine(2);
if (CheckRegistry(unloaddlls, "AlwaysUnloadDll", "1") == true)
{
guna2CheckBox2.Invoke(new Action(() =>
{
guna2CheckBox2.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox2.Invoke(new Action(() =>
{
guna2CheckBox2.Enabled = true;
}));
}
Console.WriteLine(3);
if (CheckRegistry(superfetch, "EnableSuperfetch", "0") == true)
{
guna2CheckBox4.Invoke(new Action(() =>
{
guna2CheckBox4.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox4.Invoke(new Action(() =>
{
guna2CheckBox4.Enabled = true;
}));
}
Console.WriteLine(4);
if (CheckRegistry(superfetch, "EnablePrefetcher", "0") == true)
{
guna2CheckBox5.Invoke(new Action(() =>
{
guna2CheckBox5.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox5.Invoke(new Action(() =>
{
guna2CheckBox5.Enabled = true;
}));
}
Console.WriteLine(5);
if (CheckRegistry(prioritycontrol, "Win32PrioritySeparation", "6") == true)
{
guna2CheckBox6.Invoke(new Action(() =>
{
guna2CheckBox6.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox6.Invoke(new Action(() =>
{
guna2CheckBox6.Enabled = true;
}));
}
Console.WriteLine(6);
if (CheckRegistry(disableexecuting, "DisablePagingExecutive", "1") == true)
{
guna2CheckBox7.Invoke(new Action(() =>
{
guna2CheckBox7.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox7.Invoke(new Action(() =>
{
guna2CheckBox7.Enabled = true;
}));
}
Console.WriteLine(7);
if (CheckRegistry(enablesmartscreen, "EnableSmartScreen", "0") == true)
{
guna2CheckBox8.Invoke(new Action(() =>
{
guna2CheckBox8.Enabled = false;
}));
}
else
{
CanOptimisated++;
guna2CheckBox8.Invoke(new Action(() =>
{
guna2CheckBox8.Enabled = true;
}));
}
Console.WriteLine(8);
label9.Invoke(new Action(() =>
{
label9.Text = CanOptimisated + " Пунктов";
}));
}
bool CheckRegistry(string path, string nameValue, string value)
{
var path1 = Registry.LocalMachine.OpenSubKey(path);
if (path1.GetValue(nameValue).ToString() == value)
{
path1.Close();
return true;
}
else
{
path1.Close();
return false;
}
}
启动worker的方法:
private void Form1_Load(object sender, EventArgs e)
{
guna2GradientCircleButton2.PerformClick();
RegistryChecker.RunWorkerAsync();
}
很可能在worker内部你有一个你没有捕获的异常,但是由于它是在非主线程中执行的,并且无论如何都不会期望这个主线程,所以已经出现的异常陷入了深渊,你根本看不到它。
这可以通过使用更现代的并行计算和异步操作方法来避免。
检查此代码,它应该可以正常工作或准确返回错误。这堵重复的代码墙可以通过循环来进一步简化,但我并没有把你的代码改得面目全非,只是简单地减少了它。
正如您已经了解的那样,它
BackgroundWorker
不再需要。