Windows服务中的多线程。 [英] Multi threading in windows service.

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

问题描述

我正在为客户设计一个解决方案,其中必须从SQL表中轮询数据,并将请求发送到具有由该数据形成的请求的WCF服务。 WCF服务的响应将包含必须再次在SQL表中更新的其他数据。



例如在TableA中有6列,即primaryKeyColumn,columnA,columnB,columnC,columnD和columnE。



WCF请求将包含来自A,B和C列的数据。 br />
服务响应将包含所有5列的数据。

需要为表A中的该行更新D列和E列的数据。





我想过用Windows服务实现这个。为了提高性能,我想在这个解决方案中实现多线程。



有人可以指导我吗?我之前从未在Windows服务中使用多线程。有什么风险?



我的尝试:



我创建了一个Windows服务,每隔15秒轮询一次SQL数据库表,根据特定条件获取第一行,创建WCF服务请求,调用WCF服务,根据响应更新SQL表中的数据。

我还没有实现多线程。

I am designing a solution for a client where in data has to be polled from SQL table and a request is to be sent to a WCF service with a request formed from that data. The response of the WCF service will contain additional data that has to be updated in the SQL table again.

For e.g. in TableA there are 6 columns namely primaryKeyColumn,columnA, columnB, columnC, columnD and columnE.

The WCF request will contain data from columns A,B and C.
The service response will contain data for all 5 columns.
The data of columns D and E needs to be updated for that row in the tableA.


I have thought of implementing this using Windows service. To increase the performance I am thinking to implementing multi threading in this solution.

Can some one guide me in this ? I have never used multi threading in windows service before. What are the risks ?

What I have tried:

I have created a windows service that polls the SQL database table every 15 seconds,gets the first row depending on certain condition,create the request for WCF service , call the WCF service, update the data in SQL table based on the response.
I have not implemented multi threading yet.

推荐答案

一个Windows服务基本上是多线程的,如果它想成为能够做任何事情。基本模式(其中大部分是在创建Windows服务时为您创建的)是主线程处理来自服务控制器的命令(开始,停止,暂停等)。正常模式是当开始命令通过时,你开始从你的应用程序等待某种IPC的线程。包含在该等待中的信号也来自主线程以暂停或停止服务。当IPC通过时,根据命令中涉及的处理类型,您可以在该线程中处理命令(如果它很快),或者启动另一个线程来处理命令(如果之前可以获得另一个命令)第一个是完成处理)。



所有正常的线程规则都适用 - 中断处理,保护数据等。你唯一不必担心的事情关于是否正在编组UI线程,因为没有。
A windows service basically has to be multithreaded if it wants to be able to do anything at all. The basic pattern (much of which is stubbed for you when you create a Windows Service) is that the main thread handles commands from the service controller (Start, Stop, Pause, and the like). The normal pattern is when the "Start" command goes through, you start off a thread that waits for some sort of IPC from your app. Included in that wait are also signals from the main thread to pause or stop the service. When an IPC comes through, depending on the sort of processing involved in the command, you can either process the command in that thread (if it's quick), or kick off another thread to process the command (if it's possible to get another command before the first is finished processing).

All the normal rules of threading apply - interrupting the processing, protecting data, etc. The only thing you don't have to worry about is marshalling to the UI thread, as there is none.


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

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