using System; using System.IO; using System.Threading; namespace…
- User
using System; using System.IO; using System.Threading;
namespace Ekiscopp.Modele.Generateur_appli.data { public delegate void DelegateFunction(string message);
codepublic class FileModificationListener { private string path; private FileSystemWatcher _watcher; private DelegateFunction myDelegateFunction; private Thread fmlThread; public FileModificationListener(DelegateFunction myDelegateFunction) { this.myDelegateFunction = myDelegateFunction; } public void SetPath(string path) { StopWatching(); this.path = path; fmlThread = new Thread(StartWatching); fmlThread.Start(); } public void StopWatching() { if (_watcher != null) { _watcher.EnableRaisingEvents = false; _watcher.Dispose(); fmlThread.Join(); // Wait for the thread to finish before moving on } } private void StartWatching() { string directory = Path.GetDirectoryName(path); if (!Directory.Exists(directory)) { Console.WriteLine("Invalid directory path."); return; } string filename = Path.GetFileName(path); _watcher = new FileSystemWatcher(directory, filename) { NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName, }; // Subscribe to the events of the FileSystemWatcher _watcher.Changed += OnChanged; _watcher.Created += OnChanged; _watcher.Deleted += OnChanged; _watcher.Renamed += OnRenamed; _watcher.Error += OnError; _watcher.EnableRaisingEvents = true; } private void OnChanged(object sender, FileSystemEventArgs e) { if (e.ChangeType == WatcherChangeTypes.Changed) { DateTime lastModified = File.GetLastWriteTime(e.FullPath); // Display last modification time in the format hh:mm:ss string message = "Dernière modification : " + lastModified.ToString("HH:mm:ss"); Console.WriteLine(message); // Invoke the delegate on the UI thread if necessary myDelegateFunction?.Invoke(message); } } private void OnRenamed(object sender, RenamedEventArgs e) { // Add logic for file renaming if needed } private void OnError(object sender, ErrorEventArgs e) { // Handle error events if needed Console.WriteLine("Error: " + e.GetException().Message); } } - Assistant
- User
ça me dit que le thread est arr^été instant: Le thread 0x4ee8 s'est arrêté avec le code 0 (0x0).
- Assistant
- User
using System; using System.IO; using System.Threading;
namespace Ekiscopp.Modele.Generateur_appli.data { public delegate void DelegateFunction(string message);
code - Assistant