- 爱易网页
- 
                            C#教程
- net做的windows服务程序,怎么与用户界面交互 
日期:2014-05-18  浏览次数:21179 次 
                    
                        
                         net做的windows服务程序,如何与用户界面交互?
我用.net做了个windows服务程序,网上查到了些东西,可以弹出个winform用户界面来了,现在我想在服务中放个timer,定时的写些内容,并显示在winform用户界面中的textbox1里,该怎么做?我的大致代码如下(不成功):
    public partial class Service_CARMS : ServiceBase
     {
         Form_Main f;  //与用户交互的Form窗口
         System.Threading.Thread threadform = null;
         protected override void OnStart(string[] args)
         {
             threadform = new Thread(new ThreadStart(FormShow));
             threadform.Start();
         }
        public  void FormShow()
        {
             GetDesktopWindow();
             IntPtr hwinstaSave = GetProcessWindowStation();
             IntPtr dwThreadId = GetCurrentThreadId();
             IntPtr hdeskSave = GetThreadDesktop(dwThreadId);
             IntPtr hwinstaUser = OpenWindowStation("WinSta0", false, 33554432);
             if (hwinstaUser == IntPtr.Zero)
             {
                 RpcRevertToSelf();
                 return;
             }
             SetProcessWindowStation(hwinstaUser);
             IntPtr hdeskUser = OpenDesktop("Default", 0, false, 33554432);
             RpcRevertToSelf();
             if (hdeskUser == IntPtr.Zero)
             {
                 SetProcessWindowStation(hwinstaSave);
                 CloseWindowStation(hwinstaUser);
                 return;
             }
             SetThreadDesktop(hdeskUser);
             IntPtr dwGuiThreadId = dwThreadId;
             f = new Form_Main(); //此FORM1可以带notifyIcon,可以显示在托盘里,用户可点击托盘图标进行设置
             System.Windows.Forms.Application.Run(f);
             dwGuiThreadId = IntPtr.Zero;
             SetThreadDesktop(hdeskSave);
             SetProcessWindowStation(hwinstaSave);
             CloseDesktop(hdeskUser);
             CloseWindowStation(hwinstaUser);
         }
         [DllImport("user32.dll")]
         static extern int GetDesktopWindow();
         [DllImport("user32.dll")]
         static extern IntPtr GetProcessWindowStation();
         [DllImport("kernel32.dll")]
         static extern IntPtr GetCurrentThreadId();
         [DllImport("user32.dll")]
         static extern IntPtr GetThreadDesktop(IntPtr dwThread);
         [DllImport("user32.dll")]
         static extern IntPtr OpenWindowStation(string a, bool b, int c);
         [DllImport("user32.dll")]
         static extern IntPtr OpenDesktop(string lpszDesktop, uint dwFlags,
         bool fInherit, uint dwDesiredAccess);
         [DllImport("user32.dll")]
         static extern IntPtr CloseDesktop(IntPtr p);
         [DllImport("rpcrt4.dll", SetLastError = true)]
         static extern IntPtr RpcImpersonateClient(int i);
         [DllImport("rpcrt4.dll", SetLastError = true)]
         static extern IntPtr RpcRevertToSelf();
         [DllImport("user32.dll")]
         static extern IntPtr SetThreadDesktop(IntPtr a);
         [DllImport("user32.dll")]
         static extern IntPtr SetProcessWindowStation(IntPtr a);
         [DllImport("user32.dll")]
         static extern IntPtr CloseWindowStation(IntPtr a);
         private void timer1_Tick(object sender, EventArgs e)
         {