Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6790289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:42:29+00:00 2026-05-26T17:42:29+00:00

Using C# .NET 4.0 I am trying to use a BackgroundWorker to retrieve a

  • 0

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

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:42:29+00:00Added an answer on May 26, 2026 at 5:42 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use MySql and Entity Framework, using Connector/Net 6.1 with this as
I am trying to use a COMET solution using ASP.NET . Trouble is I
I am using ASP.Net and jQuery/jQuery UI and I am trying to use the
I'm trying to use a view to create an ADO.NET entity using a view.
I am trying to use NServiceBus with an ASP.NET MVC 2 website (using VS
So I've been trying to use the Net::SSH::Multi to login to multiple machines using
I am using .Net Remoting and trying to access a remote object modified in
I'm using asp.net mvc2 and trying to send a list of json objects with
I'm trying to use protobuf in a C# project, using protobuf-net, and am wondering
I am trying to use the .NET Backgroundworker Object in an application I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.