多线程咋个监控啊?
有个文件夹A 下面有很多个文件夹 每个文件夹下面可以有文件我的需求是:监控A下面的每个文件夹中是否有没有文件,如果有,则以二进制的方式保存至数据库,然后删除,
老大说 得随时监控每个文件夹下是否有文件,所以得用多线程处理
请问各位 咋个处理啊?给我个思路吧,我第一次用多线程,没得点思路...
谢谢!
程序代码:using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using using System.Collections;
namespace Thread_FileSystemWatcher
{
class Program
{
private static Thread[] threads;
private static string[] pPath;
static void Main(string[] args)
{
threadsPEIZHI();
while (Console.Read() != 'q') ;
}
static void threadsPEIZHI()
{
try
{
pPath = new string[2];
pPath[0] = "c:\\";
pPath[1] = "e:\\";
threads = new Thread[pPath.Length];
for (int i = 0; i <= threads.Length-1; i++)
{
threads[i] = new Thread(Run);
threads[i].Name = pPath[i];
threads[i].Start();
Console.WriteLine(threads[i].Name);
}
}
catch(Exception Ex)
{
Console.WriteLine(Ex.Message);
}
}
static void Run()
{
Run(Thread.CurrentThread.Name);
}
static void Run(string pPath)
{
FileSystemWatcher fsw = new FileSystemWatcher(pPath);
fsw.Filter = "*.*";//监控所有类型,包括子文件夹
fsw.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.LastWrite;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnDeleted);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;//开启监控
}
static void OnChanged(object source,FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
static void OnCreated(object source,FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
static void OnDeleted(object source,FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
}
