Re: How to interrupt a running evaluation in MathLink
- To: mathgroup at smc.vnet.net
- Subject: [mg16937] Re: How to interrupt a running evaluation in MathLink
- From: tgayley at linkobjects.com (Todd Gayley)
- Date: Thu, 8 Apr 1999 02:32:40 -0400
- Organization: LinkObjects
- References: <7e9olb$3ku@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 5 Apr 1999 03:27:39 -0400, rmille1 at aol.com (RMille1) wrote:
>I'm using Mathlink in my C program to start a Mathlink kernel and send this
>expression to be evaluated:
>
>N[ReplaceAll["Some expression",x->"some variable"]]
>
>Everything is working otay. However, occasionally an expression I send causes
>the kernel to evaluate for longer than I would like it to. So what I want to do
>is interrupt the evaluation and abort just as it would from a command line with
>Ctr-C and abort, but through MathLink. Here's the snippet of code that will
>call it:
>
>pkt = MLFlush(Link);
>interval = 0;
>while((!MLReady(Link))&&(interval<=10))
>{
> interval++;
> Sleep(500);
>}
>
>if(interval > 10)
>{
>dump = fopen("dump.txt","a");//test
>fprintf(dump,"\n/////////////////////Restarting
>Kernel///////////////////\n\n");//test
>fclose(dump); //test
>
>***** Interrupt and Abort here. ************
>
> return 0;
>}
>
>I know there's a way to do this with MLAbort from the kernel, but not the other
>way. Also I didn't use Loopback or mprep,
You can just do this:
MLPutMessage(Link, MLAbortMessage);
MLNextPacket(Link);
// Now read the contents of this packet, which will be a
// RETURNPKT. If the computation finished just before
// the abort was recevied, it will be the result, otherwise
// it will be $Aborted
MLNewPacket(Link);