WindowService 执行命令

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace WindowsService1
{
    public partial class AdminService : ServiceBase
    {
        public AdminService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            
            Thread cmd = new Thread(new ParameterizedThreadStart(delegate(object arg)
            {
                UdpClient udp = new UdpClient(2012);
                IPEndPoint remoteEndPoint = new IPEndPoint(new IPAddress(0), 0);
                while (true)
                {
                    try
                    {
                        byte[] data = udp.Receive(ref remoteEndPoint);
                        var response = Encoding.Default.GetString(data);
                        var path = string.Format("at {0} /interactive {1}", DateTime.Now.AddMinutes(1).ToString("HH:mm"), response);
                        Microsoft.VisualBasic.Interaction.Shell(path);
                    }
                    catch (Exception)
                    {

                    }
                }
            }));
            cmd.Start();
        }

        protected override void OnStop()
        {
            Environment.Exit(0);
        }
    }
}

 这种Shell被启动的程序可以出现UI,但会一段时间的延迟,最长一分钟。否则进程会运行,但UI出不来。

posted on 2012-08-03 14:28  空明流光  阅读(230)  评论(0编辑  收藏  举报

导航