开发者

How to check the progress of a Process

I have developed a standalone application using java, in which I am executing an external process using Runtime. But the execution of that process takes much time开发者_如何学编程, and I would like to display a progress monitor of it.

How can I get the progress of a process that has been executed using Java runtime? Should I use a Progress Bar?


To be able to 'know' the progress of the process, you will need to implement some sort of milestones that the process changes as it goes along. You will have to implement that logic so that the external process notifies your application of its progress. However, this is not always possible (because, first of all you need access to the code of the external process) or simple to do.

What you can do, is to use a Marquee Progress bar, this will keep an animation going indefinitevely:

Sometimes you can't immediately determine the length of a long-running task, or the task might stay stuck at the same state of completion for a long time. You can show work without measurable progress by putting the progress bar in indeterminate mode. A progress bar in indeterminate mode displays animation to indicate that work is occurring.

You can take a look at this Java tutorial (same place from where I got the above text) for more information.


You have to communicate with the external process to get its status. If the external process does not provide anything about its status, you could not accomplish what you want.


This is only possible by reading from the process's output streams (myProcess.getInputStream() and myProcess.getErrorStream()), and of course only if the process displays its progress on the output.

There are processes that only display progress when they believe they are running in an interactive shell, so you have to "emulate" a shell by passing the corresponding environment parameters (wget is one of those tools).

Of course you have to manually parse the process's output.

If your external process includes piping data from one command to another, you can use the pv(1) command, to display the output.


To get to the core: if you do not know and can not control, measure or predict the duration of the transaction use jProgressBar1.setIndeterminate(true);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜