現時点では、MathKernel
プロセスを安全に強制終了する方法を 1 つしか知りません。この方法はNETLink
、Windows でのみ機能するようであり、Microsoft .NET 2 以降をインストールする必要があります。
killProc[processID_] := If[$OperatingSystem === "Windows",
Needs["NETLink`"];
Symbol["LoadNETType"]["System.Diagnostics.Process"];
With[{procID = processID},
killProc[procID_] := (
proc = Process`GetProcessById[procID];
proc@Kill[]
);
];
killProc[processID]
];
(*Killing the current MathKernel process*)
killProc[$ProcessID]
任意の提案や改善をいただければ幸いです。
編集:
より正しい方法:
Needs["NETLink`"];
LoadNETType["System.Diagnostics.Process"];
$kern = LinkLaunch[First[$CommandLine] <> " -mathlink -noinit"];
LinkRead[$kern];
LinkWrite[$kern, Unevaluated[$ProcessID]];
$kernProcessID = First@LinkRead[$kern];
$kernProcess = Process`GetProcessById[$kernProcessID];
AbortProtect[If[! ($kernProcess@Refresh[]; $kernProcess@HasExited),
$kernProcess@Kill[]; $kernProcess@WaitForExit[];
$kernProcess@Close[]];
LinkClose[$kern]]
編集2:
さらに正しい方法:
Needs["NETLink`"];
LoadNETType["System.Diagnostics.Process"];
$kern = LinkLaunch[First[$CommandLine] <> " -mathlink -noinit"];
LinkRead[$kern];
LinkWrite[$kern, Unevaluated[$ProcessID]];
$kernProcessID = First@LinkRead[$kern];
$kernProcess = Process`GetProcessById[$kernProcessID];
krnKill := AbortProtect[
If[TrueQ[MemberQ[Links[], $kern]], LinkClose[$kern]];
If[TrueQ[MemberQ[LoadedNETObjects[], $kernProcess]],
If[! TrueQ[$kernProcess@WaitForExit[100]],
Quiet@$kernProcess@Kill[]; $kernProcess@WaitForExit[]];
$kernProcess@Close[]; ReleaseNETObject[$kernProcess];
]
];