// Create a new FileSystemWatcher
FileProcessor fileProcessor = new FileProcessor();
FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
private void Form1_Load(object sender, EventArgs e)
{
fileSystemWatcher1.Path = @"";
fileSystemWatcher1.Filter = "";
fileSystemWatcher1.IncludeSubdirectories = false;
fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);
fileSystemWatcher1.EnableRaisingEvents = true;
}
private void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
fileProcessor.EnqueueFileName(e.FullPath);
}
}
}
FileProcessor.cs
class FileProcessor : IDisposable
{
private EventWaitHandle eventWaitHandle = new AutoResetEvent(false);
private Thread worker;
private readonly
public void EnqueueFileName(string FileName)
{
// Enqueue the file name
lock (locker) fileNamesQueue.Enqueue(FileName);
eventWaitHandle.Set();
}
private void Work(){
while (true){
string fileName = null;
lock (locker)
if (fileNamesQueue.Count > 0){
fileN
Console.WriteLine(FileName);
}
}
public partial class Form1 : Form
{
// Create a new FileS
Console.WriteLine(FileName);
}
}
1.private 改 public 在 Form1.cs new一個不就完事了
2.靜態的方式去解決也可以
3.或直接在Form1.cs新增方法