Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF使ServiceController[]可见_C#_Wpf_Binding_Observablecollection_Servicecontroller - Fatal编程技术网

C# WPF使ServiceController[]可见

C# WPF使ServiceController[]可见,c#,wpf,binding,observablecollection,servicecontroller,C#,Wpf,Binding,Observablecollection,Servicecontroller,因此,我试图制作一个数据网格,显示有关本地窗口服务的一些信息,特别是我的,我希望有服务的显示名称和状态,然后有一个按钮单击以启动或停止。我可以很好地链接button方法,但是服务状态没有改变,任何关于如何使这个属性对datagrid可见的建议,也可以根据状态动态地将button从start更改为stop,其次,如果可能的话,我想使stop命令成为button命令 有什么建议吗?您需要将服务包装到自己的类中,该类实现INotifyPropertyChanged。启动/停止服务时,引发该实例上的属性

因此,我试图制作一个数据网格,显示有关本地窗口服务的一些信息,特别是我的,我希望有服务的显示名称和状态,然后有一个按钮单击以启动或停止。我可以很好地链接button方法,但是服务状态没有改变,任何关于如何使这个属性对datagrid可见的建议,也可以根据状态动态地将button从start更改为stop,其次,如果可能的话,我想使stop命令成为button命令


有什么建议吗?

您需要将服务包装到自己的类中,该类实现INotifyPropertyChanged。启动/停止服务时,引发该实例上的属性更改事件。

您需要将服务包装到您自己的类中,该类实现INotifyPropertyChanged。启动/停止服务时,在该实例上引发属性更改事件。

很遗憾,因为
ServiceController.GetServices()
调用总是会返回一个数组,我们必须使用
dispatchermer
并在其勾号中调用
ServiceController.GetServices()
和raise notify属性已为保存服务数组的属性更改


为了便于观察而使其可见是不现实的,对吗?我们无论如何也不会从中获得任何好处。

遗憾的是,因为
ServiceController.GetServices()
调用总是会返回一个数组,我们必须有
DispatcherTimer
并在它的刻度中调用
ServiceController.GetServices()
和raise notify属性已为保存服务数组的属性更改


为了便于观察而使其可见是不现实的,对吗?无论如何,我们不会从中获得任何好处。

这就是我最后的恳求。在大多数情况下,它工作得相当好,但是我希望任何人都能提供代码建议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;

namespace v7quickbar
{
    class NotifiableServiceController : INotifyPropertyChanged
    {

        private ServiceController m_oServiceController = null;
        private System.Timers.Timer m_oServiceCheckTimer = new System.Timers.Timer();

        public ServiceControllerStatus Status { get { return this.m_oServiceController.Status; } }

        public string DisplayName { get { return this.m_oServiceController.DisplayName; } }

        public string ServiceName { get { return this.m_oServiceController.ServiceName; } }

        public bool CanStop { get { return this.m_oServiceController.CanStop; } }

        public NotifiableServiceController(ServiceController oService)
        {
            CreateObject(oService, TimeSpan.FromSeconds(.5));
        }

        public NotifiableServiceController(ServiceController oService, TimeSpan oInterval)
        {
            CreateObject(oService, oInterval);
        }

        private void CreateObject(ServiceController oService, TimeSpan oInterval)
        {
            m_oServiceController = oService;
            m_oServiceCheckTimer.Interval = oInterval.TotalMilliseconds;

            m_oServiceCheckTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_oServiceCheckTimer_Elapsed);
            m_oServiceCheckTimer.Start();
        }

        public void Start()
        {
            try
            {
                this.m_oServiceController.Start();
                this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
            }
            catch (Exception)
            {
            }
        }

        public void Stop()
        {
            try
            {
                this.m_oServiceController.Stop();
                this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
            }
            catch (Exception)
            {
            }
        }

        public void Restart()
        {
            try
            {
                if (m_oServiceController.CanStop && (m_oServiceController.Status == ServiceControllerStatus.Running || m_oServiceController.Status == ServiceControllerStatus.Paused))
                {
                    this.Stop();
                    this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
                }

                if (m_oServiceController.Status == ServiceControllerStatus.Stopped)
                {
                    this.Start();
                    this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
                }
            }
            catch (Exception)
            {
            }
        }

        void m_oServiceCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ServiceControllerStatus oCurrentStatus = m_oServiceController.Status;
            m_oServiceController.Refresh();

            if (oCurrentStatus != m_oServiceController.Status)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Status"));
            }

        }

        public static IEnumerable<NotifiableServiceController> GetServices()
        {
            List<NotifiableServiceController> oaServices = new List<NotifiableServiceController>();
            foreach (ServiceController sc in ServiceController.GetServices())
            {
                oaServices.Add(new NotifiableServiceController(sc));
            }

            return oaServices;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用System.ServiceProcess;
命名空间v7quickbar
{
类NotifiableServiceController:INotifyPropertyChanged
{
专用服务控制器m_oServiceController=null;
private System.Timers.Timer m_oServiceCheckTimer=新系统.Timers.Timer();
公共服务控制器状态{get{返回this.m_oServiceController.Status;}
公共字符串DisplayName{get{返回this.m_oServiceController.DisplayName;}}
公共字符串ServiceName{get{返回this.m_oServiceController.ServiceName;}}
public bool CanStop{get{返回this.m_oServiceController.CanStop;}}
公共NotifiableServiceController(ServiceController-oService)
{
CreateObject(oService,TimeSpan.FromSeconds(.5));
}
公共NotifiableServiceController(ServiceController-oService,TimeSpan-Interval)
{
CreateObject(oService、oInterval);
}
私有void CreateObject(ServiceController oService、TimeSpan Onterval)
{
m_oServiceController=oService;
m_oServiceCheckTimer.Interval=oInterval.total毫秒;
m_oServiceCheckTimer.appeased+=新系统.Timers.ElapsedEventHandler(m_oServiceCheckTimer_appeased);
m_oServiceCheckTimer.Start();
}
公开作废开始()
{
尝试
{
这个.m_oServiceController.Start();
this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
}
捕获(例外)
{
}
}
公共停车场()
{
尝试
{
此.m_oServiceController.Stop();
此.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
捕获(例外)
{
}
}
公共无效重新启动()
{
尝试
{
if(m_oServiceController.CanStop&(m_oServiceController.Status==ServiceControllerStatus.Running | | m_oServiceController.Status==ServiceControllerStatus.Paused))
{
这个。停止();
此.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
}
if(m_oServiceController.Status==ServiceControllerStatus.Stopped)
{
这个。Start();
this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
}
}
捕获(例外)
{
}
}
void m_oServiceCheckTimer_已过(对象发送器,System.Timers.ElapsedEventArgs e)
{
ServiceControllerStatus oCurrentStatus=m_oServiceController.Status;
m_oServiceController.Refresh();
if(oCurrentStatus!=m_oServiceController.Status)
{
调用(这是新的PropertyChangedEventArgs(“状态”);
}
}
公共静态IEnumerable GetServices()
{
List oaServices=新列表();
foreach(ServiceController.GetServices()中的ServiceController sc)
{
oaServices.Add(新的NotifiableServiceController(sc));
}
返回OAS服务;
}
公共事件属性更改事件处理程序属性更改;
}
}

这就是我最后祈求的。在大多数情况下,它工作得相当好,但是我希望任何人都能提供代码建议

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceProcess;

namespace v7quickbar
{
    class NotifiableServiceController : INotifyPropertyChanged
    {

        private ServiceController m_oServiceController = null;
        private System.Timers.Timer m_oServiceCheckTimer = new System.Timers.Timer();

        public ServiceControllerStatus Status { get { return this.m_oServiceController.Status; } }

        public string DisplayName { get { return this.m_oServiceController.DisplayName; } }

        public string ServiceName { get { return this.m_oServiceController.ServiceName; } }

        public bool CanStop { get { return this.m_oServiceController.CanStop; } }

        public NotifiableServiceController(ServiceController oService)
        {
            CreateObject(oService, TimeSpan.FromSeconds(.5));
        }

        public NotifiableServiceController(ServiceController oService, TimeSpan oInterval)
        {
            CreateObject(oService, oInterval);
        }

        private void CreateObject(ServiceController oService, TimeSpan oInterval)
        {
            m_oServiceController = oService;
            m_oServiceCheckTimer.Interval = oInterval.TotalMilliseconds;

            m_oServiceCheckTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_oServiceCheckTimer_Elapsed);
            m_oServiceCheckTimer.Start();
        }

        public void Start()
        {
            try
            {
                this.m_oServiceController.Start();
                this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
            }
            catch (Exception)
            {
            }
        }

        public void Stop()
        {
            try
            {
                this.m_oServiceController.Stop();
                this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
            }
            catch (Exception)
            {
            }
        }

        public void Restart()
        {
            try
            {
                if (m_oServiceController.CanStop && (m_oServiceController.Status == ServiceControllerStatus.Running || m_oServiceController.Status == ServiceControllerStatus.Paused))
                {
                    this.Stop();
                    this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Stopped);
                }

                if (m_oServiceController.Status == ServiceControllerStatus.Stopped)
                {
                    this.Start();
                    this.m_oServiceController.WaitForStatus(ServiceControllerStatus.Running);
                }
            }
            catch (Exception)
            {
            }
        }

        void m_oServiceCheckTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ServiceControllerStatus oCurrentStatus = m_oServiceController.Status;
            m_oServiceController.Refresh();

            if (oCurrentStatus != m_oServiceController.Status)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs("Status"));
            }

        }

        public static IEnumerable<NotifiableServiceController> GetServices()
        {
            List<NotifiableServiceController> oaServices = new List<NotifiableServiceController>();
            foreach (ServiceController sc in ServiceController.GetServices())
            {
                oaServices.Add(new NotifiableServiceController(sc));
            }

            return oaServices;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统组件模型;
使用System.ServiceProcess;
命名空间v7quickbar
{
类NotifiableServiceController:INotifyPropertyChanged
{
专用服务控制器m_oServiceController=null;