Почему поток не отрабатывает функцию FileSystemWatcher? - C#
Формулировка задачи:
Подскажите, почему поток не отрабатывает функцию FSW
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Runtime.Serialization.Formatters.Binary;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace Server_priem
- {
- class Program
- {
- static void Main(string[] args)
- {
- string ip = "127.0.0.1";
- IPHostEntry iphe = Dns.Resolve(ip);
- IPAddress ipa = iphe.AddressList[0];
- IPEndPoint ipep = new IPEndPoint(ipa, 7700);
- Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- try
- {
- s.Bind(ipep);
- s.Listen(20);
- while (true)
- {
- Socket handler = s.Accept();
- string data = null;
- while (true)
- {
- byte[] bytes = new byte[1024];
- handler.Receive(bytes);
- data += Encoding.UTF8.GetString(bytes);
- if (data.IndexOf("") > -1)
- {
- Thread t1 = new Thread(new ParameterizedThreadStart(func));
- t1.Start(data);
- Console.WriteLine(data);
- break;
- }
- }
- }
- }
- catch
- { }
- }
- static void func(object obj)
- {
- if (obj != null)
- {
- Searcher search = new Searcher(@"D:\Test", Convert.ToString(obj));
- search.Run();
- }
- }
- class Searcher
- {
- FileSystemWatcher fsw;
- public Searcher(string path, string filter)
- {
- fsw = new FileSystemWatcher(path, filter);
- fsw.Created += new FileSystemEventHandler(fsw_Changed);
- }
- public void Run()
- {
- fsw.EnableRaisingEvents = true;
- }
- void fsw_Changed(object sender, FileSystemEventArgs e)
- {
- File.Move(e.FullPath, @"D:\Test1" + e.Name);
- /*ZipFile zip = new ZipFile(@"D:\Test" + e.Name);
- zip.ExtractAll(@"D:\Test");*/
- fsw.EnableRaisingEvents = false; //отключаем слежение
- }
- }
- }
- }
Решение задачи: «Почему поток не отрабатывает функцию FileSystemWatcher?»
textual
Листинг программы
- public void Run()
- {
- fsw.EnableRaisingEvents = true;
- while(fsw.EnableRaisingEvents)
- Thread.Sleep(50);
- }
- void fsw_Changed(object sender, FileSystemEventArgs e)
- {
- fsw.EnableRaisingEvents = false; //отключаем слежение
- File.Move(e.FullPath, @"D:\Test1" + e.Name);
- zip.ExtractAll(@"D:\Test");*/
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д