Hi Erik;

  To save ya having to buy a book from a rather cheeky author :

   The answer so far as I can see is that you create a service controller in
your Windows / Taskbar / Web page app and then you call
ServiceController.CustomCommand - This passes a command directly to the
service with one parameter.  Anyhow here is some code that I just put
together and it seems to work just fine and looks to be the "official" way
to do it.

The steps I did were  -

create custom service
override onCustomCommand
add installer class to the code ( took me ages to find this out)
install service by running installutil against the exe.
write windows app
drag service name from server explorer onto the form - this creates service
controller
call servicecontroller.Executecommand(parameter)
NB parameter must be >127 or it gives an exception (took ages this too!)


Hope it helps,
and sorry for the huge legal message that gets attached :)

Jim

CODE FOLLOWS:

In the Service :

        protected override void OnStart(string[] args)  {
                SomeLogFunction("Service Running");
                SomeLogFunction("Start");
        }


        protected override void OnStop()  {
              SomeLogFunction("stop");
        }

        protected override void OnCustomCommand ( int theCmd ) {
          SomeLogFunction("OnCustomCommand");
          SomeLogFunction("Doing lots of custom stuff");
          SomeLogFunction("Command was "+theCmd);
        }


In A Windows Application :

 // This was created by dragging the name of the service in the server
explorer onto the form
 private System.ServiceProcess.ServiceController serviceController1;

  private void button1_Click(object sender, System.EventArgs e) {
    SomeLogFunction("Button1::SendCustomCommand");
    if (serviceController1.CanStop) {
      SomeLogFunction("can stop srv");
    }
    serviceController1.ExecuteCommand(150);

   }


Installer Class To Get Install to work (pasted into service code):

  [RunInstaller(true)]
  public class ServiceRegister: Installer {
    private ServiceInstaller serviceInstaller;
    private ServiceProcessInstaller processInstaller;

    public ServiceRegister() {
      // define and create the service installer
      serviceInstaller = new ServiceInstaller();
      serviceInstaller.StartType = ServiceStartMode.Manual;


      serviceInstaller.ServiceName = "AMyService";
//ServiceControl.ServiceControlName;
      serviceInstaller.DisplayName = "A Descrpttion";
//ServiceControl.ServiceControlDesc;
      Installers.Add(serviceInstaller);

      // define and create the process installer
      processInstaller = new ServiceProcessInstaller();
      #if true //RUNUNDERSYSTEM
         processInstaller.Account = ServiceAccount.LocalSystem;
      #else
      // prompt for user and password on install
      processInstaller.Account = ServiceAccount.User;
      processInstaller.Username = null;
      processInstaller.Password = null;
      #endif
      Installers.Add(processInstaller);
    }
  }

-----Original Message-----
From: Erick Thompson [mailto:[EMAIL PROTECTED]]
Sent: 08 June 2002 01:59
To: [EMAIL PROTECTED]
Subject: [DOTNET] UI for Windows service


I have a windows service which I'm trying to write control code for. What I
want is to put an icon in the system tray, and have a context menu on the
icon. Then, when the user selects a menu item, either a form is shown, or
some action is taken, depending on the menu item selected. I have a problem,
and a couple of questions.

My problem is that I can't seem to get any events handled from the
NotifyIcon. In the OnStart method, I create a NotifyIcon, and add a onclick
handler. In the click handler, I add a simple MessageBox.Show("click"). I
compile the service, add it with InstallUtil, and set the "Allow Service to
Interact with Desktop" option in the control panel. I start it up, and the
NotifyIcon appears correctly, but I can't get anything in the event handler
to run. I've tried a number of variations, all with no luck. I've searched
all over the net, and found people with this same problem, but no solutions.
What am I doing wrong?

My questions are related to managing the service. I want to have a fairly
complex form to manage the service, which is started from the context menu.
Should I put this form in a completely different assembly then the service,
and/or call it from a different thread? What is the best practice of writing
Windows services, and the UI that controls them?

If anyone has an example project doing I'm working on, that would be very
helpful.

Thanks,
Erick

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.


***********************************************************************

The information contained in this E-mail is confidential and may be subject to legal 
privilege. Access to this E-mail by anyone other than the intended recipient is 
unauthorised.If you are not the intended recipient, you must not use, copy, distribute 
or disclose the E-mail or any part of its contents or take any action in reliance on 
it. If you have received this E-mail in error, please notify us immediately by E-mail 
or telephone. All reasonable precautions have been taken to ensure no viruses are 
present in this E-mail. As Clerical Medical cannot accept responsibility for loss or 
damage arising from the use of this E-mail or attachments we recommend that you 
subject these to your virus checking procedures prior to use.

Part of the HBOS Group
Clerical Medical Investment Group Limited
Registered Office 33 Old Broad Street
London EC2N 1HZ

Registered in England and Wales, Registered No. 3196171
Regulated by the Financial Services Authority.  A member of ABI.
For staff training and security purposes E-mail communications and telephone calls may 
be monitored or recorded.
***********************************************************************

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to