首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

net做的windows服务程序,怎么与用户界面交互

2012-08-02 
net做的windows服务程序,如何与用户界面交互?我用.net做了个windows服务程序,网上查到了些东西,可以弹出个

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)
  {
  //MessageBox.Show("mm");
  ////////经调试发下以下代码异常,提示


/////"调用的目标发生了异常,..........{"未将对象引用设置到对象的实例
  f.ShowInf("触发器触发"); //调用与用户交互的Form里的方法,该方法附加字符串到textbox1中。
   
  }
  }

救命啦

[解决办法]
timer用system的timer,不用form的timer
[解决办法]
怎么我看的都说不行呢。

例如:

http://stackoverflow.com/questions/5836977/show-status-window-from-windows-service
[解决办法]
既然带界面了,你为啥还选择window服务去做这样的事情呢?何不直接采用form程序?
[解决办法]

探讨
既然带界面了,你为啥还选择window服务去做这样的事情呢?何不直接采用form程序?

[解决办法]
分离2个程序,用socket/pipe/WINDOWS MESSAGE/MSMQ通讯
[解决办法]
最好单起个进程通过管道或SOCKET与服务进行通信,一方面操作更方便,另外还要保证winform把服务的可靠性。
[解决办法]
System.Threading.Timer

热点排行