c# - BacgroundWorkerCompleted 不执行

标签 c# multithreading backgroundworker

使用 C#.NET 4.0

我正在尝试使用 BackgroundWorker 检索远程计算机上的服务列表。这是作为后台工作人员运行的,因此 UI 在等待信息时保持响应。

我的做法是将信息的请求放在后台worker的_DoWork方法中,使用ManualResetEvent.Set()表示信息已经处理完毕。应在 _RunWorkerCompleted 方法中设置 ManaualResetEvent。 _RunWorkerCompleted 从不执行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Threading;
using System.Diagnostics;

namespace DemoTest
{

    public partial class Form1 : Form
    {
        List<ServiceController> MyBox1Services;

        ManualResetEvent MyBox1ServicesObtained = new ManualResetEvent(false);


        public Form1()
        {
            InitializeComponent();

        }



        private void button1_Click(object sender, EventArgs e)
        {

            // Use background worker to get the list of services in the background
            BackgroundWorker MyBox1Servicess = new BackgroundWorker();
            MyBox1Servicess.DoWork += new DoWorkEventHandler(bwGetMyBox1Services_DoWork);
            MyBox1Servicess.RunWorkerCompleted += new RunWorkerCompletedEventHandler(MyBox1Servicess_RunWorkerCompleted);




            MyBox1Servicess.RunWorkerAsync();

            // Wait until all the services have been obtained before moving on

            MyBox1ServicesObtained.Reset();

            MyBox1ServicesObtained.WaitOne();


            // Update the GUI Here....


        }


        void MyBox1Servicess_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                MessageBox.Show("Canceled!");
            }

            else if (!(e.Error == null))
            {
                MessageBox.Show(e.Error.Message);
            }

            else
            {
                Debug.WriteLine("MyBox1Servicess_RunWorkerCompleted");
            }
            MyBox1Services = (List<ServiceController>)e.Result;
            MyBox1ServicesObtained.Set();
        }


        void bwGetMyBox1Services_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            ServiceController[] allServices;
            List<ServiceController> allServicesList = new List<ServiceController>();

            try
            {
                allServices = ServiceController.GetServices("MyBox1");
                foreach (ServiceController sc in allServices)
                {
                    allServicesList.Add(sc);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            e.Result = allServicesList;
        }

    }
}

最佳答案

这是因为 RunWorkerCompleted 事件在 UI 线程上触发,您通过 ManualResetEvent 调用 WaitOne 在按钮单击处理程序中阻止了该事件>.'

与其使用 ManualResetEvent,不如在 RunWorkerCompleted 事件处理程序中更新您的 GUI?

关于c# - BacgroundWorkerCompleted 不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8085792/

相关文章:

c# - hunspell 拼写检查

c# - FileSystemWatcher 不会为在 ffmpeg 中打开的文件触发事件

android - 如何检查是否在 Android 的 UI 线程上运行?

c# - 有没有办法在.NET Core FilterAttribute 中获取请求正文?

java - 在锁定对象上同步与在处理的对象上同步

c++ - 多线程时一直调用对象析构函数,但对象没有超出范围

ASP.NET - 在没有 HttpContext.Current 的情况下获取网站的 URL(在后台线程中运行)

C#/Winform : Enter data into HTML page, 提交表单

c# - 为什么我的程序在取消BackgroundWorker后执行?

c# - 设置 SortedDictionary 的第 i 个值