RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1199436
Accepted
mounten1
mounten1
Asked:2021-11-03 19:26:54 +0000 UTC2021-11-03 19:26:54 +0000 UTC 2021-11-03 19:26:54 +0000 UTC

BackgroundWorker 未完全执行。C#

  • 772

是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();
        }
c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    aepot
    2021-11-03T20:39:48Z2021-11-03T20:39:48Z

    很可能在worker内部你有一个你没有捕获的异常,但是由于它是在非主线程中执行的,并且无论如何都不会期望这个主线程,所以已经出现的异常陷入了深渊,你根本看不到它。

    这可以通过使用更现代的并行计算和异步操作方法来避免。

    private async void Form1_Load(object sender, EventArgs e)
    {
        IProgress<Action> callback = new Progress<Action>(action => action()); // заменитель инвока
        try
        {
            guna2GradientCircleButton2.PerformClick();
            await Task.Run(() => RegistryChecker(callback));
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    private void RegistryChecker(IProgress<Action> status)
    {
        int CanOptimisated = 0;
        //var onedrive = Registry.LocalMachine;
    
        const string printerspath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\RemoteComputer\NameSpace\{863aa9fd-42df-457b-8e4d-0de1b8015c60}";
        const string unloaddlls = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer";
        const string superfetch = @"SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\PrefetchParameters";
        const string prioritycontrol = @"SYSTEM\CurrentControlSet\Control\PriorityControl";
        const string disableexecuting = @"SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management";
        const string enablesmartscreen = @"SOFTWARE\Policies\Microsoft\Windows\System";
    
        Debug.WriteLine(1); // используйте вместо Console в GUI приложениях
        bool check1 = false;
        try
        {
            // RegistryKey - IDisposable, с ним можно вот так.
            using (RegistryKey printers = Registry.LocalMachine.OpenSubKey(printerspath))
            {
                printers.GetValue("");
            }
            CanOptimisated++;
            check1 = true;
        }
        catch { }
        status.Report(() => guna2CheckBox1.Enabled = check1); // было 15 инвоков, стало 8
    
        Debug.WriteLine(2);
        bool check2 = !CheckRegistry(unloaddlls, "AlwaysUnloadDll", "1");
        if (check2)
            CanOptimisated++;
        status.Report(() => guna2CheckBox2.Enabled = check2);
    
        Debug.WriteLine(3);
        bool check4 = !CheckRegistry(superfetch, "EnableSuperfetch", "0");
        if (check4)
            CanOptimisated++;
        status.Report(() => guna2CheckBox4.Enabled = check4);
    
        Debug.WriteLine(4);
        bool check5 = !CheckRegistry(superfetch, "EnablePrefetcher", "0");
        if (check5)
            CanOptimisated++;
        status.Report(() => guna2CheckBox5.Enabled = check5);
    
        Debug.WriteLine(5);
        bool check6 = !CheckRegistry(prioritycontrol, "Win32PrioritySeparation", "6");
        if (check6)
            CanOptimisated++;
        status.Report(() => guna2CheckBox6.Enabled = check6);
    
        Debug.WriteLine(6);
        bool check7 = !CheckRegistry(disableexecuting, "DisablePagingExecutive", "1");
        if (check7)
            CanOptimisated++;
        status.Report(() => guna2CheckBox7.Enabled = check7);
    
        Debug.WriteLine(7);
        bool check8 = !CheckRegistry(enablesmartscreen, "EnableSmartScreen", "0");
        if (check8)
            CanOptimisated++;
        status.Report(() => guna2CheckBox8.Enabled = check8);
    
        Debug.WriteLine(8);
        status.Report(() => label9.Text = CanOptimisated + " Пунктов");
    }
    
    private bool CheckRegistry(string path, string nameValue, string value)
    {
        // этот метод тоже немного упростился
        using (RegistryKey path1 = Registry.LocalMachine.OpenSubKey(path))
        {
            return path1.GetValue(nameValue).ToString() == value;
        }
    }
    

    检查此代码,它应该可以正常工作或准确返回错误。这堵重复的代码墙可以通过循环来进一步简化,但我并没有把你的代码改得面目全非,只是简单地减少了它。

    正如您已经了解的那样,它BackgroundWorker不再需要。

    • 3

相关问题

  • 使用嵌套类导出 xml 文件

  • 分层数据模板 [WPF]

  • 如何在 WPF 中为 ListView 手动创建列?

  • 在 2D 空间中,Collider 2D 挂在玩家身上,它对敌人的重量相同,我需要它这样当它们碰撞时,它们不会飞向不同的方向。统一

  • 如何在 c# 中使用 python 神经网络来创建语音合成?

  • 如何知道类中的方法是否属于接口?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何从列表中打印最大元素(str 类型)的长度?

    • 2 个回答
  • Marko Smith

    如何在 PyQT5 中清除 QFrame 的内容

    • 1 个回答
  • Marko Smith

    如何将具有特定字符的字符串拆分为两个不同的列表?

    • 2 个回答
  • Marko Smith

    导航栏活动元素

    • 1 个回答
  • Marko Smith

    是否可以将文本放入数组中?[关闭]

    • 1 个回答
  • Marko Smith

    如何一次用多个分隔符拆分字符串?

    • 1 个回答
  • Marko Smith

    如何通过 ClassPath 创建 InputStream?

    • 2 个回答
  • Marko Smith

    在一个查询中连接多个表

    • 1 个回答
  • Marko Smith

    对列表列表中的所有值求和

    • 3 个回答
  • Marko Smith

    如何对齐 string.Format 中的列?

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5