Есть BackgroundWorker
который выполняет некий код. После окончания работы, он должен написать переменную в label
. Но это не происходит. Тогда я решил разбить код в BackgroundWorker
на маленькие кусочки, после выполнения кусочка кода выводится число кусочка (Console.WriteLine
), их всего 8. Первый семь - это проверка условий, последний - вывод. То когда я скомпилировал программу, то в выводе увидел:
1
2
3
4
5
6
7
C чем это связано?
Код воркера:
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;
}
}
Метод, который запускает воркера:
private void Form1_Load(object sender, EventArgs e)
{
guna2GradientCircleButton2.PerformClick();
RegistryChecker.RunWorkerAsync();
}
BackgroundWorker
очень старый, и использовать его неудобно. С очень большой вероятностью ваш код можно написать проще, но вы его не показали. Покажите весь метод, где воркер запускается и весь его обработчик целиком. А так, ваша проблема за пределами показанного кода.