Windows服务中的问题 [英] Problem in windows service
本文介绍了Windows服务中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,大家好,
我在窗口服务中遇到问题,它会触发一个错误,例如
:成员名称不能与其封闭类型MyFirstWindowsService相同
我的代码:
hello Good morning EveryOne,
i have problem in window service it''s Fire one error like
:member names cannot be the same as their enclosing type MyFirstWindowsService
my Code:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
namespace MyFirstWindowsService
{
public class MyFirstWindowsService
{
static void Main()
{
ServiceBase[] ServicesToRun;
Debugger.Break();
ServicesToRun = new ServiceBase[]
{
new MyFirstWindowsService()
};
ServiceBase.Run(ServicesToRun);
}
public partial class MyFirstWindowsService : ServiceBase
{
public MyFirstWindowsService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer.Enabled = true;
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
protected override void OnStop()
{
}
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
System.Timers.Timer timer = new System.Timers.Timer();
public void MyFirstWindowsService()//Problem is Here
{
InitializeComponent();
this.CanStop = true;
this.CanPauseAndContinue = true;
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
{
System.Console.WriteLine("myTimer event occurred");
//Here we can write beautiful code that performs some our own tasks.
}
}
}
}
推荐答案
好像您给几个方法使用了相同的名称,但是您不能那样做,程序会完全被它弄糊涂.
Looks like you gave several methods the same name, you cant do that, the program would get totally confused by it.
更改您的部分类名称及其构造函数名称,因为主类和部分类的名称不能相同.试试这个:
Hi,
Change your partial class name and its constructor name, because the nae of the main class and partial class cannot be same. Try this:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
namespace MyFirstWindowsService
{
public class MyFirstWindowsService
{
static void Main()
{
ServiceBase[] ServicesToRun;
Debugger.Break();
ServicesToRun = new ServiceBase[]
{
new Test()
};
ServiceBase.Run(ServicesToRun);
}
public partial class Test : ServiceBase
{
public Test()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
timer.Enabled = true;
timer.Interval = 10000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
}
protected override void OnStop()
{
}
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
System.Timers.Timer timer = new System.Timers.Timer();
public void MyFirstWindowsService()//Problem is Here
{
InitializeComponent();
this.CanStop = true;
this.CanPauseAndContinue = true;
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
protected void timer_Elapsed(object source, System.Timers.ElapsedEventArgs aa)
{
System.Console.WriteLine("myTimer event occurred");
//Here we can write beautiful code that performs some our own tasks.
}
}
}
}
祝一切顺利.
-=-提交
All the best.
-=-Amit
这篇关于Windows服务中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文