1

I have a FileSystemEventHandler that onchange read the data from my file, now I need to return this data as I am working with a handler. now my code works but does not return anything so I do not have the updated data on my front end. this is my question : how can I return the data?

Thanks

public static string data = null;
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static string Run()
{
    try
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        //watcher.Path = System.IO.Directory.GetCurrentDirectory();
        watcher.Path = Path.Combine(HttpRuntime.AppDomainAppPath, "view");
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "info.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }
    catch (Exception ex)
    {
        Console.Write(ex.ToString());
    }
    return data;

}

private static void OnChanged(object source, FileSystemEventArgs e)
{
    data = FileManager.Read();
}
lol
  • 93
  • 3
  • 11
  • Do you mean you want `Run()` to block until `OnChanged()` has been raised? – CodeCaster Nov 01 '16 at 15:42
  • Your run method will return with data == null long before the `OnChanged` event fires to set `data`. – ChrisF Nov 01 '16 at 15:43
  • If you don't need `Run()` to be blocked then you should not expect it to return data to update ui. You should update your ui either directly in `OnChanged()` or in another method that must be called in OnChanged() and that will read the data for you. – CodingNagger Nov 01 '16 at 15:44
  • What kind of front-end you have?, you must have [an Observer, Pub/Sub, and Data Binding](http://stackoverflow.com/questions/15594905/difference-between-observer-pub-sub-and-data-binding) – Byron Nov 01 '16 at 15:45
  • @Byron I am using angularJS a function to invoke the method using handler and return the daa – lol Nov 01 '16 at 16:14
  • you are using a local varible for the `FileSystemWatcher`, so when the Run` method end the watcher will be release, it must be also static or be stored on a program lifecycle – Byron Nov 01 '16 at 16:21

1 Answers1

1

FileSystemWatcher is an event-driven mechanism. You don't need to return anything from your Run() method - you need to do whatever you want to do as a result of the change in your OnChanged() event handler. Try taking a look at the API for FileSystemEventArgs.

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
    try
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        //watcher.Path = System.IO.Directory.GetCurrentDirectory();
        watcher.Path = Path.Combine(HttpRuntime.AppDomainAppPath, "view");
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        watcher.Filter = "info.txt";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);

        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }
    catch (Exception ex)
    {
        Console.Write(ex.ToString());
    }
}


private static void OnChanged(object source, FileSystemEventArgs e)
{
    string fileText = File.ReadAllText(e.FullPath);
    // do whatever you want to do with fileText
}
Daniel A. Thompson
  • 1,904
  • 1
  • 17
  • 26
  • so how can I return my data , in this case handler won't be invoked – lol Nov 01 '16 at 15:57
  • The `FileSystemWatcher` will invoke `OnChanged()` for you when the change occurs. It's then up to you to do whatever you want to do in the handler as a result of the change. You could update your UI directly from the handler, for example. – Daniel A. Thompson Nov 01 '16 at 15:59