日期:2014-05-18  浏览次数:20907 次

C#做windows服务的问题
怎么才能让服务打开指定窗体??最好有示例。。。帮帮忙吧~~

------解决方案--------------------
用服务也不难
我做的一个监控程序,调试的时候需要查看用系统事件日志
C# code

        public void MonitorProcess()
        {
            // Create the source, if it does not already exist.
            if (!EventLog.SourceExists("MySource"))
            {
                EventLog.CreateEventSource("MySource", "MyNewLog");

            }
            EventLog myLog = new EventLog();
            myLog.Source = "MySource";

            List<String> CurrentSystemProcessList = new List<String>();
            CurrentSystemProcessList.Clear();
            Process[] localAll = Process.GetProcesses();
            //EventLog myLog = new EventLog("ServiceTest", ".", "ServiceTest");
            StringBuilder msg = new StringBuilder("");
            for (int i = 0; i < localAll.Length; i++)
            {
                if (localAll[i].ProcessName == "Rever")
                {
                    //localAll[i].Close();
                    try
                    {
                        bool IsCloseMainWindow = false;
                        IsCloseMainWindow=localAll[i].CloseMainWindow();
                        if(!IsCloseMainWindow)
                        {

                            msg.Remove(0, msg.Length);
                            msg.Append(DateTime.Now.ToString());
                            msg.Append(" 进程调用CloseMainWindow,没有成功!");
                            myLog.WriteEntry(msg.ToString());

                            localAll[i].Kill();
                        }
                        localAll[i].WaitForExit();
                        //this.Stop();
                        Process.Start(@"D:\Rever.exe");
                        msg.Remove(0, msg.Length);
                        msg.Append(DateTime.Now.ToString());
                        msg.Append(" 程序执行成功!");
                        myLog.WriteEntry(msg.ToString());
                    }
                    catch (Win32Exception ee)
                    {
                        msg.Remove(0, msg.Length);
                        msg.Append(DateTime.Now.ToString());
                        msg.Append(" 退出程序失败!");
                        myLog.WriteEntry(msg.ToString());
                    }
                }
            }                

        }

------解决方案--------------------
看看这篇文章
http://blog.joycode.com/chinahuman/archive/2008/05/04/115103.aspx