Skip to main content
bug fix in the code. The author forgot the initial Manipulate[ in the second example.
Source Link
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])
 ]
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])
 ]
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])
 ]
added 1430 characters in body
Source Link
Nasser
  • 150.6k
  • 12
  • 162
  • 376

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.

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])
 ]

enter image description here

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.

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])
 ]

enter image description here

added i
Source Link
Nasser
  • 150.6k
  • 12
  • 162
  • 376

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
     ]
   )
 ]

enter image description here

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},
     Do[PrimeQ[i];
      Pause[0.1];
      k = k + 1;
      track = k/n;
      If[abort, result = -1; Break[]]
      , {i, n}
      ];
     
     result
     ]
   )
 ]

enter image description here

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
     ]
   )
 ]

enter image description here

Source Link
Nasser
  • 150.6k
  • 12
  • 162
  • 376
Loading