如果将新文件添加到fodler,则启动Windows服务 [英] Start windows service if a new file is added to the fodler

查看:72
本文介绍了如果将新文件添加到fodler,则启动Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想在文件夹中添加新的XML文件时触发Windows服务启动(使用filesystemwatcher)



1.监控文件夹 - 如果添加了新的xml文件

2.启动服务 - 在服务中执行操作。

3.停止服务

4.创建日志

5并将xml文件移动到存档。



请帮我一个示例代码。



谢谢,

Deepthi。



我尝试了什么:



我试过这个

如何在C#中实现简单的filewatcher Windows服务 [< a href =https://www.codeproject.com/Articles/18521/How-to-implement-a-simple-filewatcher-Windows-servtarget =_ blanktitle =New Window> ^ ]

推荐答案

您的设计效率低下,不必要地增加了项目的复杂性。



我会这样做一个不使用FileSystemWatcher的多线程服务。



主线程将监视有问题的文件夹中的新文件(我会使用React / Rx库) 。



当检测到新文件时,它将启动另一个线程(或异步任务)来执行需要对该文件执行的任何操作。



使用线程的原因是因为你永远不知道处理文件需要多长时间,而且你不想延迟对后续文件的处理。 br />


为此提供适当的示例代码将真正超过Q / A的目的。您应该做的是实际上至少尝试一次自己编写代码,并在遇到问题时来这里。
Your design is inefficient and needlessly increases project complexity.

I would do it with ONE multi-threaded service that does NOT use FileSystemWatcher.

The main thread would monitor the folder in question for new files (I would use React/Rx library for this).

When a new file is detected, it would start another thread (or an async task) to perform whatever action needed to be performed on the file.

The reason for using threads is because you never know how long it will take to process the file, and you don't want to delay processing on subsequent files.

Providing appropriate "sample code" for this would really exceed what Q/A is intended for. What you should do is actually make at least one attempt to write the code yourself, and come here when you have questions.


这篇关于如果将新文件添加到fodler,则启动Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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