启动服务(通过GUI)使前端无响应

时间:2015-12-10 14:34:43

标签: c# service remote-desktop

我很遗憾对C#编程很新,并且从未使用过GUI编码,因此我可能会遵循非常糟糕的做法。以下是我正在做的事情。

总体目标是从机器中提取状态,这样我就可以告诉前端服务是否已经启动而没有实际远程进入机器。任何建议都会有所帮助!

我很抱歉,代码片段似乎与我的命名空间和类调用无关。

namespace General
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Password = "password";
            options.Username = "username";
            InitializeComponent();
        }
    public void Start(string programname)
    {
        textBox.Text = "Starting";
        ServiceController svc = new ServiceController("service name", "remote desktop name");
        svc.Start();
        svc.WaitForStatus(ServiceControllerStatus.Running, timer1);
        if (svc.Status == ServiceContrllerStatus.Running)
        {
            textBox8.Text = "Success";
        }
        else
        {
        textBox8.Text = "Fail";
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        Start(button.Text);
    }
}
}

1 个答案:

答案 0 :(得分:0)

试试这个。

Thread myThread = new Thread(() =>
        {
            ServiceController svc = new ServiceController("service name", "remote desktop name");
            svc.Start();
            svc.WaitForStatus(ServiceControllerStatus.Running, timer1);
            if (svc.Status == ServiceContrllerStatus.Running)
            {
                Dispatcher.Invoke(() =>
                { //This is required to acccess the Main UI Thread If you don't do this you will get and error
                    textBox8.Text = "Success";
                });

            }
            else
            {
                Dispatcher.Invoke(() =>
                {
                    textBox8.Text = "Fail";
                });

            }
        });
        myThread.Start();