如何在Firefox中异步监视文件? [英] How can I monitor a file asynchronously in Firefox?

查看:133
本文介绍了如何在Firefox中异步监视文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视Windows机器上的纯文本文件以进行更改。每当外部应用程序更新文件,我的插件应该读取它并采取相应的行动。

通过相当多的文档,我找不到一个直接的工具来做这在Firefox中。所以我想出了以下方法。

请建议。



方法1 -
在a while循环{

a)列出受监视目录中的所有文件/使用nsIFile.exists()

b)如果文件存在,请转到下一步否则转到睡眠

c)使用nsIFile接口并获得文件& amp; amp; amp; gt;的属性lastModifiedTime比较它与最初存储的值

d)睡眠1秒。

}

我打算使用NetUtil.asyncFetch()来读取这个文件。
路障是a)绕过安全性b)在主线程中调用时性能受到影响。

虽然这在实际意义上不是异步的,但是我可以在ChromeWorkerThread中将其称为.js。 / p>

试过上面的方法..

但是我睡不着..好像没有办法睡觉.. setTimeOut()没有帮我



方式2 -

小小的牵扯,不知道是否会起作用:)

打开'nsIFile' nsIInputStreamPump'如果文件存在

解决方案

没有内置的API来获取有关文件更改的通知。但是,您可以使用 js-ctypes 直接调用OS函数。所以在Windows上,你可以创建一个 ChromeWorker 并且调用 FindFirstChangeNotification function 。然后您可以使用以下调用的循环:


  • WaitForSingleObject - 等待改变在您正在观看的目录中。

  • 如有必要,请将更改通知发送到主线程。 microsoft.com/en-us/library/windows/desktop/aa364427%28v=vs.85%29.aspxrel =nofollow> FindNextChangeNotification



您应该致电 FindCloseChangeNotification 完成观看。


I want to monitor a plain text file on windows machine for changes to it. Whenever external application updates the file, my add-on is supposed to read it and act accordingly.

Going through quite bit of documentation I could not find a direct facility to do this in Firefox. So I came up the following approaches.

Please suggest.

Approach 1 - In a while loop {
a) List all the files in the directory being monitored / Use nsIFile.exists()
b) If files does exist go to next step else go to sleep
c) use "nsIFile" interface and obtain the attribute "lastModifiedTime" of the file & compare it with value stored initially
d) Sleep for 1 second.
}
I intend to use NetUtil.asyncFetch() for reading this file. Roadblocks being a) bypassing security b) performance hit when called in main thread.
Though this is not asynchronous in real sense .. I can call this as .js in "ChromeWorker" Thread

Tried above approach ..
but I could not sleep .. seems there is no way to sleep .. setTimeOut() did not help me

Approach 2 -
Little far fetched, not sure if this will work :)
Open the 'nsIFile' as 'nsIInputStreamPump' if file exists

解决方案

There is no built-in API to get notified about file changes. However, you could use js-ctypes to call OS functions directly. So on Windows you would create a ChromeWorker and call FindFirstChangeNotification function there. You would then use a loop with the following calls:

And you should call FindCloseChangeNotification once you are done watching.

这篇关于如何在Firefox中异步监视文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆