windows服务中OnStart(string args)中的参数怎么传递,劳驾,急!!!

解决方案 »

  1.   

    使用 OnStart 指定服务收到“开始”命令时进行的处理。OnStart 是您在其中指定服务行为的方法。OnStart 能以参数作为传递数据的方式,但这种用法很少见。
    不要使用构造函数执行应在 OnStart 中进行的处理。使用 OnStart 处理服务的所有初始化。构造函数在应用程序的可执行文件运行(而不是在服务运行)时调用。可执行文件在 OnStart 之前运行。例如,当继续服务时,不会再次调用构造函数,因为 SCM 已在内存中保存了该对象。如果 OnStop 释放在构造函数(而不是 OnStart)中分配的资源,则第二次调用服务时不会再次创建所需资源。
     服务可以设置为在计算机重新启动时启动,方法是在服务的安装程序上设置 StartType。在此类情况下,会在系统启动时调用 OnStart。应在派生类中重写 OnStart。要使服务可用,应在服务类中将 OnStart 和 OnStop 都实现。        // Start the service.
            protected override void OnStart(string[] args)
            {
                IntPtr handle = this.ServiceHandle;
                myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
                SetServiceStatus(handle, myServiceStatus);            // Start a separate thread that does the actual work.            if ((workerThread == null) ||
                    ((workerThread.ThreadState &
                     (System.Threading.ThreadState.Unstarted | System.Threading.ThreadState.Stopped)) != 0)){                workerThread = new Thread(new ThreadStart(ServiceWorkerMethod));
                    workerThread.Start();
                }
                myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
                SetServiceStatus(handle, myServiceStatus);        }