日期:2014-05-16  浏览次数:20745 次

我编了一个Windows服务程序,为什么程序无法启动?
这是一个壁纸自动换程序,不让启动,哪位高手帮我看一下

启动服务的时候提示:
本地计算机上的Service_Windows服务启动后停止,某些服务在未由其它服务或程序使用时将自动停止。

我用这些代码照般到一个窗口程序中就完全正常,就是编成Windows服务程序就无法启动

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.IO;

namespace Service_Windows
{
    public partial class Service_Windows : ServiceBase
    {
        public Service_Windows()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            huanbizhi hbz = new huanbizhi();
            hbz.huan(@"H:\3号机备分\bak\照片", 5, 0);
        }

        protected override void OnStop()
        {
        }
    }
    public class huanbizhi
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);

        [DllImport("user32.dll")]
        public static extern bool SetSysColors(int cElements, int[] lpaElements, int[] lpaRgbValues);




        public void huan(string path, int time, int yangshi)
        {
            string[] filename = Directory.GetFiles(path);
            Random rd = new Random();
            bool b = true;
            while (b)
            {
                ChangeWallPaper(filename[rd.Next(0, filename.Length - 1)], yangshi);
                System.Threading.Thread.Sleep(time * 1000);
            }
        }
        public void ChangeWallPaper(string picturePath, int style)