【发布时间】:2016-11-01 15:39:55
【问题描述】:
我有一个FileSystemEventHandler onchange 从我的文件中读取数据,现在我需要在使用处理程序时返回这些数据。现在我的代码可以工作但没有返回任何东西,所以我的前端没有更新的数据。
这是我的问题:我怎样才能返回data?
谢谢
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();
}
【问题讨论】:
-
您的意思是要阻止
Run()直到OnChanged()被提出? -
在
OnChanged事件触发设置data之前很久,您的运行方法将返回 data == null。 -
如果您不需要阻止
Run(),那么您不应该期望它返回数据来更新用户界面。您应该直接在OnChanged()中更新您的用户界面,或者在必须在 OnChanged() 中调用的另一种方法中更新您的用户界面,该方法将为您读取数据。 -
你有什么样的前端?,你一定有an Observer, Pub/Sub, and Data Binding
-
@Byron 我正在使用 angularJS 一个函数来调用使用处理程序的方法并返回 daa
标签: c# file filesystemwatcher