简体   繁体   English

通过Windows Service启动和停止并发线程

[英]Starting and Stopping concurrent threads through Windows Service

I am new to .NET environment. 我是.NET环境的新手。 I need a windows service whose Start and Stop method will call a specific class, say MainContext's Start and Stop method respectively. 我需要一个Windows服务,该服务的StartStop方法将调用特定的类,分别说MainContext的Start和Stop方法。 The MainContext class will contain the list of extensions loaded through MEF . MainContext类将包含通过MEF加载的扩展列表。 Each extensions needs to be run as a separate thread in concurrent manner. 每个扩展都需要以并发方式作为单独的线程运行。

The class MainContext will contain two public methods Start and Stop , both of return type void and containing no arguments. MainContext类将包含两个公共方法StartStop ,它们的返回类型均为void并且不包含任何参数。 On calling Start method, it will start the thread for each extensions loaded through MEF in an concurrent manner. 在调用Start方法时,它将以并发方式为通过MEF加载的每个扩展启动线程。 While the Stop method will stop all the threads started by Start method. Stop方法将停止由Start方法启动的所有线程。

Which is the best threading practice in this situation? 在这种情况下最好的线程实践是什么? I am confused. 我很困惑。 Should I use System.Threading.Tasks.Task class or System.Threading.Thread class for multi-threading or should I use any other technique? 我应该使用System.Threading.Tasks.Task类还是System.Threading.Thread类进行多线程处理,还是应该使用其他任何技术? Which would be better suited to my requirements and would provide me more flexibility and stability? 哪个更适合我的要求,并为我提供更大的灵活性和稳定性?

I've done something similar using MAF, with add-ins watching file servers, databases, and web services. 我使用MAF进行了类似的操作,并使用外接程序监视文件服务器,数据库和Web服务。 The Windows service invokes Start and Stop methods in each add-in, which in turn enable or disable System.Timer.Timers. Windows服务会在每个外接程序中调用Start和Stop方法,从而启用或禁用System.Timer.Timers。 The actual processing is done on each timer's elapsed event handler, which runs on a pooled thread. 实际处理是在每个计时器的经过的事件处理程序上完成的,该处理程序在池线程上运行。

One of the add-ins watches a database table that's used inter-process communication so that I can monitor and control the other add-ins from a UI app. 其中一个插件监视用于进程间通信的数据库表,这样我就可以通过UI应用程序监视和控制其他插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM