简体   繁体   English

视窗。 服务多个实例处理来自同一单个目录重复的文件

[英]Windows. Service multiple instances processing files from same single directory duplication

I have .net windows service which gets list of image files from a folder and do some conversion and sent the converted files to another directory. 我有.net Windows服务,该服务从文件夹获取图像文件列表并进行一些转换,然后将转换后的文件发送到另一个目录。 I want achive more throughput by adding another instance of serVice watching same folder. 我想通过添加另一个观看同一文件夹的serVice实例来实现更高的吞吐量。 I want 2 instances process files independently without any duplicate processing. 我希望2个实例独立处理文件,而不进行任何重复处理。 What patterns can be used? 可以使用哪些模式? Is file locking would work for this ? 文件锁定是否可以解决此问题? Don't want to use database or any other messaging platform. 不想使用数据库或任何其他消息传递平台。 I Can use text files etc to create synchronization if needed. 如果需要,我可以使用文本文件等创建同步。

  1. If using .net I would consider creating multiple threads (using TPL in .net) that would be used to process the files in parallel. 如果使用.net,我将考虑创建多个线程(在.net中使用TPL),这些线程将用于并行处理文件。 This way you have a single process that has control over the entire process. 这样,您就可以控制整个过程。 Hence no need to track what process (exe) is processing a file, no databases, no locking, etc.. 因此,无需跟踪哪个进程(exe)正在处理文件,没有数据库,没有锁定等。

  2. However if you wish to have multiple processes processing the files, then one option of synchronizing the processing is to make use of a Mutex. 但是,如果您希望有多个进程来处理文件,则同步处理的一种方法是使用互斥锁。

I would use this option along with Solution 1. Ie use TPL (multiple threads) in one service. 我将在解决方案1中使用此选项。即在一项服务中使用TPL(多线程)。 And also use Mutexes. 并且也使用Mutexes。 This way you have the benefit of multiple threads and multiple services. 这样,您可以受益于多个线程和多个服务。 Hopefully this is what you are after. 希望这就是你所追求的。

https://msdn.microsoft.com/en-us/library/bwe34f1k(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/bwe34f1k(v=vs.110).aspx

Before processing any file, create a Mutex with a particular name and if ownership has been granted, then continue processing the file. 在处理任何文件之前,请创建一个具有特定名称的Mutex,如果已授予所有权,则继续处理该文件。 If ownership hasn't been granted you can safely assume that another process or another thread (within the same application) has acquired a lock on this Mutex, meaning another process/thread is already processing the file. 如果未授予所有权,则可以放心地假设另一个进程或另一个线程(在同一应用程序内)已获取此互斥锁的锁,这意味着另一个进程/线程已在处理文件。

Sample code: 样例代码:

var fileMutex = new Mutex(true, "File Name", out mutexWasCreated);
if (mutexWasCreated){
    //Some other process/thread is processing this file, so nothing to do
}
else {
    //Start processing the file
}

If one service (exe) goes down, then the threads would die, meaning the mutexes would be released and those files will be available for processing by another process. 如果一个服务(exe)发生故障,则线程将死亡,这意味着互斥体将被释放,并且这些文件将可供另一进程处理。

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

相关问题 与Windows服务相同的应用程序的多个实例? - Multiple Instances of same Application as a Windows Service? 如何在 C# 中允许来自同一目录的单实例应用程序但来自不同目录的多个实例? - How to allow single instance application from the same directory but several instances from different directories in C#? 使用 Inno Setup 安装具有不同配置的同一 Windows 服务的多个实例 - Installing multiple instances of the same Windows service with different configuration using Inno Setup MSMQ,从单个Windows服务读取多个队列消息 - MSMQ ,reading multiple Queue messages from a single windows Service 同一异步任务的多个实例(Windows Phone) - Multiple instances of the same Async task (Windows Phone) C#通过同一类的多个实例处理一堆数据 - C# Processing bunch of data by multiple instances of same class 在同一目录中查找多个文件 - Find multiple files in the same directory 同一进程的多个实例记录在不同的文件上 - Multiple instances of same process logging on different files 从相同的Windows服务重新启动Windows服务 - restart windows service from the same windows service Service Fabric-在同一Web服务的多个实例之间同步配置 - Service Fabric - Sync configuration between multiple instances of the same web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM