none
serviceBase programming RRS feed

  • Question


  • Hi;

    I have a active control that I want when  the server start is started and always run in background on the server  ,(it retrieve some information in the network and store in DB), but I want  a UI that user can start and stop it, so I want a serviceBase program that has UI, I know the window service program can't interact with user but if I use a serviceBase program and application it should  both program use this controller, how pass this control from service Base to application program,

    And another problem is that I can't new instance from this active component in window service program , and if I declare it as static variable when I start it in onStart method in serviceBase program ,I have this error,

      System.TypeInitializationException was unhandled

    Message: The type initializer for 'MyNewService.Service1' threw an exception.

     

    Please guide me;

    Thanks;

    Monday, March 1, 2010 11:46 AM

All replies

  • Running ActiveX controls in a Windows Service is not the easiest thing to do.

    You'll have to create a thread in your OnStart and have that thread create a Windows Forms form containing the ActiveX control, and then enter the Windows Forms message processing loop (Application.Run).

    In OnStop, you'll have to Invoke over to the Winforms thread and tell the main form to Close.

    For a UI, the best way to handle this is to have some form of inter-process communications between the service and the UI. Named pipes are a common option, though I usually use WCF with a netNamedPipe binding.

           -Steve


    Programming blog: http://nitoprograms.blogspot.com/
      Including my TCP/IP .NET Sockets FAQ
      and How to Implement IDisposable and Finalizers: 3 Easy Rules
    Microsoft Certified Professional Developer

    How to get to Heaven according to the Bible
    Monday, March 1, 2010 2:49 PM

  • hi
    thanks for your reply but i didn't understand very  well.
    i create a windows form  that contain activeX and in the serviceBase class i create a thread that call this form.
    my code is

      void ThreadTask()
            {
                while (true)
                {
                    Application.Run(new Form1());
                    Thread.Sleep(100);
                }
            }
      
      protected override void OnStart(string[] args)
         {       
                Thread trd = new Thread(new ThreadStart(this.ThreadTask));
                trd.IsBackground = true;
                trd.Start();
         }
    is it correct?
    how does write onStop method?

    sorry i'm new in serviceBase, with this code when i start my service i have this error:

    the windowService1 service on local computer and then stopped. some services stop automatically if they have no work to do for example the performance logs and alerts service

    onstart method do in 30 sec so how call a infinite thread?

    and i did't understand your secound solution

    best regards
    Tuesday, March 2, 2010 7:15 AM