Home > Software design >  Update context from object function
Update context from object function

Time:11-12

In my window, I need to set a list of FileWatcher, for a list on machines. For this I set a new class MachineWatcher :

public class MachineWatcher : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propName)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        private bool NotifyPropertyChanged<T>(ref T variable, T valeur, [CallerMemberName] string nomPropriete = null)
        {
            if (object.Equals(variable, valeur)) return false;

            variable = valeur;
            NotifyPropertyChanged(nomPropriete);
            return true;
        }
        private Machine machine { get; set; }
        public Machine Machine { get { return this.machine; } set { this.machine = value; } }
        private string typeWatcher { get; set; } = "";
        public string TypeWatcher { get { return this.typeWatcher; } set { this.typeWatcher = value; } }
        FileSystemWatcher watcher { get; set; }
        public FileSystemWatcher Watcher { get { return this.watcher; } set { this.watcher = value; } }
        private string lastFileName { get; set; } = "";
        private DateTime lastEvent { get; set; } = DateTime.MinValue;
        private ObservableCollection<string> listErrors { get; set; } = new ObservableCollection<string>();
        public ObservableCollection<string> ListErrors { get { return this.listErrors; } set { this.listErrors = value; NotifyPropertyChanged("ListErrors"); } }
        public MachineWatcher()
        {

        }
        public MachineWatcher(string type,string directoryStock,string fileFilter)
        {
            this.typeWatcher = type;
            this.watcher = new FileSystemWatcher
            {
                Path = Path.GetDirectoryName(directoryStock),
                Filter = fileFilter
            };
            if (this.typeWatcher=="stock")
            {
                this.watcher.Created  = new FileSystemEventHandler(OnStockFileCreated);
            }
            else if(this.typeWatcher=="machine")
            {
                //this.watcher.NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.CreationTime;
                this.watcher.Changed  = OnFeedBackNesterCreated;
                this.watcher.Created  = OnFeedBackNesterCreated;
                this.watcher.Renamed  = OnFeedBackNesterCreated;
                this.watcher.Deleted  = OnFeedBackNesterCreated;
            }
            this.watcher.EnableRaisingEvents = true;
        }
        private void OnFeedBackNesterCreated(object source, FileSystemEventArgs e)
        {
            try
            {
                //try to read file
            }
            catch
            {
                string errorText = "Error reading file "   e.FullPath;
                this.ListErrors.Add(errorText);
            }
        }
        
    }

Then in my mainWindow I defined a List<MachineWatcher>

I create then like that :

MachineWatcher machineWatcher = new MachineWatcher("stock", directoryStock, "*.csv");
this.listMachineWatchers.Add(machineWatcher);

What I would like, is when my MachineWatcher meets an error, and go in catch, update a Observable<string> ListErrors, where are written all the errors for all machines.

Is there a way to call a function in MainWindows from the object MachineWatcher?

CodePudding user response:

Is there a way to call a function in MainWindows from the object MachineWatcher?

Yes, assuming you have a reference to the MainWindow in MachineWatcher.

You could for example inject the MachineWatcher class with a reference to MainWindow when you instantiate it:

private readonly MainWindow mainWindow;

public MachineWatcher(string type,string directoryStock,string fileFilter, MainWindow mainWindow)
{
    this.typeWatcher = type;
    this.watcher ...
    this.mainWindow =  mainWindow;
}

MainWindow:

MachineWatcher machineWatcher = new MachineWatcher("stock", directoryStock, "*.csv", this);

Then you can call any member of mainWindow directly from the MachineWatcher class.

  • Related