GUIKit の例題:ThreadedStopCounter
GUIKit の例題:ThreadedStopCounter
GUIKitで提供されていた機能は,組込みのWolfram言語にネイティブのインターフェース構造およびコントロール関数で利用できるようになった.
Needs["GUIKit`"]ThreadedStopCounter[] :=
GUIRun[
Widget["Panel", WidgetGroup[{
Widget["Button", {
"text" -> "Run",
BindEvent["action",
Script[
SetPropertyValue[{"button", "enabled"}, False, InvokeThread -> "Dispatch"];
SetPropertyValue[{"cancelButton", "enabled"}, True, InvokeThread -> "Dispatch"];
CheckAbort[
Do[SetPropertyValue[{"label", "text"}, "Current value is " <> ToString[i], InvokeThread -> "Dispatch"], {i, 300, 0, -1}], $Aborted];
SetPropertyValue[{"label", "text"}, "Press the button to count from 300 to 0.", InvokeThread -> "Dispatch"];
SetPropertyValue[{"button", "enabled"}, True, InvokeThread -> "Dispatch"];
SetPropertyValue[{"cancelButton", "enabled"}, False, InvokeThread -> "Dispatch"];
], InvokeThread -> "New"]
}, Name -> "button"],
Widget["Button", {
"text" -> "Cancel",
"enabled" -> False,
BindEvent["action",
InvokeMethod[{"ScriptEvaluator", "abort"}],
InvokeThread -> "New"]},
Name -> "cancelButton"],
Widget["Label", {
"text" -> "Press the button to count from 300 to 0."},
Name -> "label"]
}, WidgetLayout -> Row] ]
]例題
この例題は,ユーザインターフェース関数のInvokeThreadオプションを使って計算をスレッドし,ユーザインターフェースの更新要求がイベント処理スレッドで起るようにすることで,大規模な計算を行っている最中にユーザインターフェースの更新要求をどのように可視化するかを示すものである.
また,一旦プロセスがスレッドされると,大規模なWolfram言語の計算を同じユーザインターフェースから放棄することも可能になる.放棄は,現行カーネルの評価中止にカーネルスクリプトの評価を必要としないBindEvent["action",InvokeMethod[{"ScriptEvaluator","abort"}]を使って放棄をトリガすることで実行される.
計算を適切に中断する別の重要な機能にCheckAbort[]の呼出しを適切な場所に置いてScriptブロックの特定の場所で中止を行い,Scriptの残りの部分が期待通りに続行されるというものがある.GUIKit のフレームワークではScriptブロックはすべて自動的にAbortProtectでラップされるので,デフォルトで,すべてのスクリプトが中断なしに評価される.より長い計算を行うスクリプトにはCheckAbortブロックを加えるべきであろう.
ThreadedStopCounter[]