Windows 服务 +从数据库读取
我是 Windows 服务的新手。我需要一个 Windows 服务来从数据库的表中读取条目。我有一个控制台应用程序,我在其中添加新项目 WINDOWS SERVICE。我已经有一个访问数据库的方法和其他方法。我可以在启动时放置一个读取数据库的线程。我把线放在哪里? (我该怎么做)。我在 WINDOWS SERVICE 上的哪里添加这些方法?我有这样的Windows服务:
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
do
{
thread.start();
bool variab = readFromDatabase (Database table);
}
}
protected override void OnStop()
{
}
I am new To windows service. I need a windows service that reads an entry from a table from database. I have a CONSOLE APP where I add new project WINDOWS SERVICE. I already have a method that access the database, and other methods. I can put a thread on start that reads the database. Where do I put the thread? ( how can I do that). Where on WINDOWS SERVICE I add those methods? I have the Windows Service like this:
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
do
{
thread.start();
bool variab = readFromDatabase (Database table);
}
}
protected override void OnStop()
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您创建一个类,在其中执行您需要的所有操作并在服务中创建:
这使您有机会与服务分开运行和测试您的类,也许在调试发布期间。
I suggest that you create a class in which you do everything you need and create in in the service:
This gives you opportunity to run and test your class separate from service, maybe during debug release.
对于 Windows 服务,通常会创建一个方法来在单独的线程中执行服务的主循环。否则服务可能会变得无响应。例如,您可以使用名为 MainLoop 的方法来执行服务逻辑。 OnStart 方法仅用于执行初始化任务,例如读取配置值或启动服务的线程。并使用 OnStop 执行清理任务、停止线程等...
With windows services usually a method is created to execute the main loop of the service, in a separated thread. Otherwise the service could become unresponsive. For example, you can have a method called MainLoop to execute the service logic. Use the OnStart method only to do the initializing tasks, such as read configuration values or start the threads of the service. And use the OnStop to executing cleaning tasks, stopping threads, etc...
您必须将包含数据访问逻辑的代码或类放在 OnStart 方法中
You must put your code or class, which contain data access logic in OnStart method