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();
}
6
  • Do you mean you want Run() to block until OnChanged() has been raised? Commented Nov 1, 2016 at 15:42
  • Your run method will return with data == null long before the OnChanged event fires to set data. Commented Nov 1, 2016 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. Commented Nov 1, 2016 at 15:44
  • What kind of front-end you have?, you must have an Observer, Pub/Sub, and Data Binding Commented Nov 1, 2016 at 15:45
  • @Byron I am using angularJS a function to invoke the method using handler and return the daa Commented Nov 1, 2016 at 16:14

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

so how can I return my data , in this case handler won't be invoked
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.