Dar*_*fer 1 c# multithreading backgroundworker
使用C#.NET 4.0
我正在尝试使用BackgroundWorker来检索远程计算机上的服务列表.这是作为后台工作程序运行的,因此UI在等待信息时保持响应.
我的方法是将信息请求放在后台工作程序的_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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是因为RunWorkerCompleted事件是在UI线程上触发的,您在按钮点击处理程序中通过ManualResetEvent调用来阻止该线程WaitOne.
而不是使用ManualResetEvent,为什么不只是在RunWorkerCompleted事件处理程序中更新您的GUI ?