Table of Contents

Class FileDependency

Namespace
Cuemon.Runtime
Assembly
Cuemon.Core.dll

Provides a way to monitor any changes occurred to one or more files while notifying subscribing objects.

public class FileDependency : Dependency, IDependency
Inheritance
FileDependency
Implements
Inherited Members

Examples

FileDependency defers FileWatcher creation until monitoring begins by accepting a Lazy<FileWatcher> factory. This example creates a temporary settings.json file, wraps a FileWatcher in a Lazy<> with a 500ms polling period, and passes it to FileDependency with breakTieOnChanged: true. Key steps include checking lazyWatcher.IsValueCreated before and after StartAsync to confirm deferred creation, subscribing to DependencyChanged, and inspecting HasChanged after signaling. Console output shows the watcher creation status, BreakTieOnChanged value, and the changed state.

using System;
using System.IO;
using System.Threading.Tasks;
using Cuemon.Runtime;

namespace MyApp.Examples;

public static class FileDependencyExample
{
    public static async Task DemonstrateAsync()
    {
        string filePath = Path.Combine(Environment.CurrentDirectory, "settings.json");
        File.WriteAllText(filePath, "{ }");

        var lazyWatcher = new Lazy<FileWatcher>(() => new FileWatcher(filePath, false, options =>
        {
            options.Period = TimeSpan.FromMilliseconds(500);
        }));

        var dependency = new FileDependency(lazyWatcher, breakTieOnChanged: true);
        dependency.DependencyChanged += static (_, e) => Console.WriteLine(e.UtcLastModified.ToString("O"));

        Console.WriteLine(lazyWatcher.IsValueCreated);
        Console.WriteLine(dependency.BreakTieOnChanged);

        await dependency.StartAsync();

        Console.WriteLine(lazyWatcher.IsValueCreated);
        Console.WriteLine(dependency.HasChanged);
    }
}

Constructors

FileDependency(IEnumerable<Lazy<FileWatcher>>, bool)

Initializes a new instance of the FileDependency class.

public FileDependency(IEnumerable<Lazy<FileWatcher>> lazyFileWatchers, bool breakTieOnChanged = false)

Parameters

lazyFileWatchers IEnumerable<Lazy<FileWatcher>>

The FileWatcher sequence to associate with this dependency.

breakTieOnChanged bool

if set to true all FileWatcher instances is disassociated with this dependency after first notification of changed.

Remarks

The sequence of FileWatcher initializations is deferred until StartAsync() is invoked.

FileDependency(Lazy<FileWatcher>, bool)

Initializes a new instance of the FileDependency class.

public FileDependency(Lazy<FileWatcher> lazyFileWatcher, bool breakTieOnChanged = false)

Parameters

lazyFileWatcher Lazy<FileWatcher>

The FileWatcher to associate with this dependency.

breakTieOnChanged bool

if set to true all FileWatcher instances is disassociated with this dependency after first notification of changed.

Remarks

The FileWatcher initialization is deferred until StartAsync() is invoked.

Exceptions

ArgumentNullException

lazyFileWatcher cannot be null.

See Also