passer une fonction en paramétre à une classe c#
- User
passer une fonction en paramétre à une classe c#
- Assistant
- User
y'a des erreurs dans mon code?
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace Ekiscopp.Modele.Generateur_appli.data { public delegate void DelegateFunction(String message); public class FileModificationListener { private string path; private FileSystemWatcher _watcher; private DelegateFunction myDelegateFunction;
public FileModificationListener(DelegateFunction myDelegateFunction) { this.myDelegateFunction = myDelegateFunction; } public void setPath(string path) { try { StopWatching(); } catch (NullReferenceException) { // do nothing } this.path = path; StartWatching(); } public void StopWatching() { _watcher.EnableRaisingEvents = false; _watcher.Dispose(); } public void StartWatching() { Task.Factory.StartNew(() => { _watcher = new FileSystemWatcher(path) { NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName, }; }); // Abonnement aux évenements du FileSystemWatcher _watcher.Changed += onChanged; _watcher.Created += onChanged; _watcher.Deleted += onChanged; _watcher.Renamed += onRenamed; _watcher.Error += onError; _watcher.EnableRaisingEvents = true; try { Task.Delay(-1).Wait(); } catch { } } private void onChanged(object sender, FileSystemEventArgs e) { DateTime lastModified = File.GetLastWriteTime(e.FullPath); // on veut afficher dernière modification à hh:mm:ss String message = "Dernière modification: " + lastModified.ToString("hh:mm:ss"); myDelegateFunction(message); } - Assistant
- User
j'arrête pas d'avoir une erreur qui me dit que le chemmin n'est pas valide alors que c'est un chemin vers un fichier excel que je choisis par moi même avec un openfiledialog
- Assistant
- User
est ce que c'est possible de faire la même chose que ma fonction mais pour surveiller les modifications sur un fichier et non un dossier
- Assistant
- User
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace Ekiscopp.Modele.Generateur_appli.data { public delegate void DelegateFunction(String message); public class FileModificationListener { private string path; private FileSystemWatcher _watcher; private DelegateFunction myDelegateFunction; private Task currentTask; public FileModificationListener(DelegateFunction myDelegateFunction) { this.myDelegateFunction = myDelegateFunction; }
public void setPath(string path) { StopWatching(); this.path = path; StartWatching(); } public void StopWatching() { if (_watcher != null) { _watcher.EnableRaisingEvents = false; _watcher.Dispose(); } } public void StartWatching() { if (currentTask != null) { // stop la tache currentTask.Wait(); } currentTask = new Task(() => { string directory = Path.GetDirectoryName(path); string filename = Path.GetFileName(path); _watcher = new FileSystemWatcher(directory, filename) { NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName, }; // Abonnement aux évenements du FileSystemWatcher _watcher.Changed += onChanged; _watcher.Created += onChanged; _watcher.Deleted += onChanged; _watcher.Renamed += onRenamed; _watcher.Error += onError; _watcher.EnableRaisingEvents = true; try { Task.Delay(-1).Wait(); } catch { } }); currentTask.Start(); } private void onChanged(object sender, FileSystemEventArgs e) { DateTime lastModified = File.GetLastWriteTime(e.FullPath); // on veut afficher dernière modification à hh:mm:ss String message = "Dernière modification: " + lastModified.ToString("hh:mm:ss"); Console.WriteLine(message); myDelegateFunction(message); } private void onRenamed(object sender, RenamedEventArgs e) { } private void onError(object sender, ErrorEventArgs e) { } } - Assistant
- User
en fait si j'utilise pas de task ça s'execute pas en parallèle et ça freeze mon programme dcp
- Assistant