Windows服务问题 [英] windows service problem

查看:92
本文介绍了Windows服务问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是帮助我的电话.服务启动时我要消息.

ya it''s help tel to me. i want message when service starts . can u tel how it''s work code ..

推荐答案

> 因此,您需要创建一个计时器并将其间隔设置为您要清除目录的持续时间.
并启动Windows服务上的计时器将启动.
因此,在那时调用Timer Elapsed事件时,可以调用deletefiles方法,以便删除文件..
请查看以下解决方案,它将为您提供一个很好的主意,我要说的是您的意思...

For every 1 minute you want to delete your files from your directory m i right..?
so you need to create a timer and set its interval to your time duration on which you want to clear your directory.
and start that timer on windows service will start.
so when timer Elapsed event is call at that time you can call your deletefiles method so your files will be deleted..
please see the below solutions it will give you a good idea what i mean to say you...

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.IO;
using System.Windows.Forms;

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer t;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Display();
        }

        protected void CreateTimer()
        {
            t = new System.Timers.Timer();
            t.Interval = (10000) * 6;  // here i will set it to 60 secs to call deletefiles method
            t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
        }
        protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            DeleteFiles();
        }

        protected override void OnStop()
        {
           
        }

        public void Display()
        {
            MessageBox.Show("Service Started");
            CreateTimer();
            t.Enabled = true;
            t.Start();

        }
        protected void DeleteFiles()
        {
            string[] Files = Directory.GetFiles(@"YOUR DIRECTORY PATH");
            for (int i = 0; i < files.length; i++)
            { 
                File.Delete(Files[i]); 
            }
        }
    }
}



希望这对您有帮助...



Hope this will help you...


你好朋友...
首先查看此链接:

简单Windows服务示例 [ 3)然后按键盘上的Enter键,它将打开服务管理器
4)按照上述说明中的步骤4和5开始服务.


或者,您也可以使用安装项目的已编译代码来启动服务.

但是,必须先在计算机上安装该代码,然后才能运行您的服务.

您也可以在Windows启动时使服务自动启动,以便在重新启动计算机后启动.您可能要在Google上进行研究.


希望对您有所帮助.
Hello Friend...
First of all see this link:

Simple Windows Service Sample[^]

After installing your service you need to start it. You can do this by following these steps:

1) Open Control Panel
2) Then Administrative Tools
3) Services
4) Find your service
5) Right click on it and select start

6) Now your service will start and be working.

Alternatively you can:
1) Press WindowsKey + R
2) In Run type in services.msc
3) Then hit enter on your keyboard and it will open the service manager
4) Follow steps 4 and 5 of the instructions above to start your service.


Or you can also start your service with the compiled code of your setup project.

However, before that code can run your service must be installed on your computer.

You can also make your service as start automatically when Windows starts so that it will start after you restart your computer. You may want to research this on Google.


I hope this will help you..


SAISAGAR nalla
是的,它可以帮助您联系我.服务启动时我要消息.你可以打电话给它的工作代码..


1)将System.Windows.Form的引用添加到您的项目中
>转到参考右键单击选择添加参考
>从.net中选择System.Windows.Form,然后按Enter

2)像这样在Windows服务中添加一个方法
SAISAGAR nalla
ya it''s help tel to me. i want message when service starts . can u tel how it''s work code ..


1) Add Reference of System.Windows.Form to your project
> Go To reference Right Click Choose Add reference
> Chose from .net select System.Windows.Form hit enter

2) add one Method to your Windows Service like this
public void display()
{
    System.Windows.Forms.MessageBox.Show("Service Start");
}



3)像这样在您的服务onStart方法中调用该方法



3) Call that Method in your Service onStart Method like this

protected override void OnStart(string[] args)
{
    display();
}



因此,当您在那时启动服务时,它将显示一个消息框,其中包含诸如服务启动"之类的消息



so when you start your service at that time it will show you a messagebox with message like "Service Start"


这篇关于Windows服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆