Here is an example of using ProgressIndicator
. I added Pause[]
to simulate some blocking operation, and added Abort button. This is the pattern that I use when using this feature. It is simple and works for me. The idea is to pass the tracking symbol by reference.
Manipulate[(*example using progress monitor in Manipulate*)
Module[{result},
abort = False; track=0;
result = solve[n, Unevaluated[track], Unevaluated[abort]];
track = 0;
If[abort, "Aborted", "success!"]],
Grid[{
{Control[{{n, 10, "size of problem?"}, 1, 1000, 1, Appearance -> "Labeled"}]},
{Button["Abort", abort = True, ImageSize -> {40, 30}]},
{Dynamic[ProgressIndicator[track, {0, 1}]]} }],
{{track, 0}, None},
{{abort, False}, None},
TrackedSymbols :> {n},
SynchronousUpdating -> False,
ContinuousAction -> False,
Initialization :>
(
solve[n_, track_, abort_] := Module[{k = 0, result = 1,i},
Do[PrimeQ[i];
Pause[0.1];
k = k + 1;
track = k/n;
If[abort, result = -1; Break[]]
, {i, n}
];
result
]
)
]
If you like to see position of progress, it is probably easier to use one of the new version 9 controls such as BulletGauge
since those will automatically label the display. Here is example using [BulletGauge][2]
and this works without Refresh
even, so I removed the passing of the track
symbol as in the first example above. I used reference 1 for this example just for now. Help has more information on how to set this control in more specific ways.
Manipulate[
Module[{result}, abort = False; track = 0;
result = solve[n];
track = 0;
If[abort, "Aborted", "success!"]],
Grid[{{Control[{{n, 10, "size of problem?"}, 1, 1000, 1, Appearance -> "Labeled"}]},
{Button["Abort", abort = True,ImageSize -> {40, 30}]},
{Dynamic[BulletGauge[track, 1, Range[0, 1]]]} (*more options in help*)
}
],
{{track, 0}, None},
{{abort, False}, None},
TrackedSymbols :> {n},
SynchronousUpdating -> False,
ContinuousAction -> False, Initialization :> (
solve[n_] := Module[{k = 0, result = 1, i},
Do[PrimeQ[i];
Pause[0.1];
k = k + 1;
track = k/n;
If[abort, result = -1; Break[]], {i, n}];
result])
]
monitor_boxes
. $\endgroup$