简体   繁体   English

进度表和XPCOM

[英]Progress Meter and XPCOM

I'm developing a Firefox extension that uses PyXPCOM to run a process. 我正在开发使用PyXPCOM来运行进程的Firefox扩展。 I would like to have a progress meter that is shown when the process is started and gives feedback to the user. 我希望有一个进度表,该进度表在流程启动时会显示出来并向用户提供反馈。

In the javascript I have called the Thread Manager to run the process in Python: 在javascript中,我调用了线程管理器以在Python中运行进程:

 var threadManager = Components.classes["@mozilla.org/thread-manager;1"].getService();
 var background = threadManager.newThread(0);
 background.dispatch(obj, background.DISPATCH_NORMAL);

so I wonder whether there is a way to check when the thread starts its job and when it finishes. 所以我想知道是否有一种方法可以检查线程何时开始工作以及何时完成。 This helps me to control my progress meter in javascript! 这可以帮助我在javascript中控制进度表!

If anyone has better idea in implementing the progress meter, please let me know :) 如果有人对实现进度表有更好的主意,请告诉我:)

Thanks 谢谢

You shouldn't be creating new threads from JavaScript directly - this has lots of thread safety issues and from all I know this functionality is no longer available in Firefox 4. The replacement are chrome workers: https://developer.mozilla.org/en/DOM/ChromeWorker . 您不应该直接从JavaScript创建新线程-这存在很多线程安全问题,并且从我所知,Firefox 4中不再提供此功能。替换为chrome worker: https//developer.mozilla.org/ zh / DOM / ChromeWorker So you would create your worker like this: 因此,您将像这样创建您的工作程序:

var worker = new ChromeWorker("script.js");
worker.postMessage(obj);

You would also want to receive messages from the worker (eg progress notifications). 您还希望接收来自工作人员的消息(例如进度通知)。

worker.onmessage = function(event)
{
    if (event.data.type == "progress")
         alert("Worker progress: " + event.data.value);
    else if (event.data.type == "done")
         alert("Worker done!");
}

The worker itself would need to send you progress notifications via postMessage function of course, eg: 工作者本身当然需要通过postMessage函数向您发送进度通知,例如:

postMessage({type: "progress", value: done/total*100});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM