새 파일을 만들 때 SFTP 디렉토리에서 파일을 구문 분석 할 C# 콘솔 응용 프로그램을 만들어야합니다. 이를 위해 새 파일 경로를 대기열에 추가하고 새 스레드를 만들고 파일을 구문 분석하는 FileCreated 이벤트로 FileSystemWatcher를 구현했습니다.C# 콘솔 응용 프로그램에서 FileSystemWatcher와 Timer를 다중 스레드로 구현하는 방법은 무엇입니까?
필자는 FileSystemWatcher가 새로운 파일을 감지하지 못할 수도 있다는 블로그를 읽었습니다. 매주 1 시간마다 발생하는 Timer를 구현하고 FileSystemWatcher 스레드가 대기 상태에 있으면 IMCOMING SFTP 폴더를 읽고 구문 분석합니다 파일.
다음은 FileSystemWatcher 및 Timer 용으로 작성한 코드입니다.하지만 제대로 작동하지 않으며 filesystemwatcher가 다중 스레딩에 없다고 생각합니다. 올바른 해결책을 얻도록 도와주세요.
주요
static void Main(string[] args)
{
try
{
string path = incomingFilePath;
if (Directory.Exists(path))
{
#region Initiate Timer
Thread t = new Thread(new ParameterizedThreadStart(ThreadLoop));
t.Start((Action)fileProcessor.StartTimer);
#endregion
#region FileSystemWatcher
watcher = new FileSystemWatcher { Path = path, Filter = "*.CUST", IncludeSubdirectories = true };
watcher.Created += new
FileSystemEventHandler(watcher_FileCreated);
watcher.Error += new
ErrorEventHandler(watcher_OnError);
watcher.EnableRaisingEvents = true;
#endregion
}
}
catch (Exception Err)
{
}
}
FileSystemWatcher 구성 CODE :
private static void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
if (e.FullPath.ToUpper().Contains("INCOMING"].ToString()))
{
fileProcessor.EnqueueFile(e.FullPath);
lock (lockObject)
{
files.Enqueue(path);
}
if (FileWacherThread == null || shouldStop)
{
FileWacherThread = new Thread(new ThreadStart(Work));
FileWacherThread.Start();
}
// If the thread is waiting then start it
else if (FileWacherThread.ThreadState == ThreadState.WaitSleepJoin)
{
waitHandle.Set();
}
}
}
private void Work()
{
while (!shouldStop)
{
string path = String.Empty;
lock (lockObject)
{
if (files.Count > 0)
{
path = files.Dequeue();
}
}
if (!String.IsNullOrEmpty(path))
{
// Process the file
ParseFile(path);
}
else
{
// If no files are left to process then wait
waitHandle.WaitOne();
}
}
}
TIMER 코드
public void StartTimer()
{
lock (lockObject)
{
if (FileWacherThread == null || FileWacherThread.ThreadState == ThreadState.WaitSleepJoin)
{
if (files.Count == 0)
{
IEnumerable<string> result = new List<string>(Directory.GetFiles(incomingFilePath, "*.CUST", SearchOption.AllDirectories)).Where(s => s.Contains(incomingFilePrefix));
foreach (string path in result)
{
ParseFile(path);
}
}
}
}
}
실제로 작동하지 않는 것은 무엇입니까? – fixagon
btw, 고객 파일이 큰 경우 타이머 코드는 임시 파일을 먼저 작성하고 완료되면 * .CUST로 이름을 변경하지 않는 한 여전히 작성중인 파일을 찾을 수 있습니다. – mtijn