BacgroundWorkerCompleted 不执行

发布于 2024-12-15 06:35:38 字数 2760 浏览 0 评论 0原文

使用 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;
        }

    }
}

Using C# .NET 4.0

I am trying to use a BackgroundWorker to retrieve a list of the services on a remote computer. This is run as a background worker so the UI remains responsive while waiting for the information.

My approach is to place the request for information in the _DoWork method of the background worker, and use ManualResetEvent.Set() to indicate that the information has been processed. The ManaualResetEvent should be set in the _RunWorkerCompleted method. _RunWorkerCompleted never executes.

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;
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2024-12-22 06:35:38

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

为什么不在 RunWorkerCompleted 事件处理程序中更新 GUI,而不是使用 ManualResetEvent

This is because the RunWorkerCompleted event is firing on the UI thread, which you blocked in the button click handler with the ManualResetEvent call to WaitOne.'

Rather than use the ManualResetEvent, why not just update your GUI in the RunWorkerCompleted event handler?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文