public static bool cliBlockUntilEvent(PlTerm blockOn, PlTerm maxTime, PlTerm testVarsCode, PlTerm exitCode)
{
WaitUntilDelegateList list = null;
object getInstance1 = GetInstance(blockOn);
var wud = getInstance1 as WaitUntilDelegate;
if (wud == null)
{
if (!(getInstance1 is WaitUntilDelegateList))
{
return(Error("Not an instance of WaitUntilDelegate: " + blockOn));
}
list = getInstance1 as WaitUntilDelegateList;
}
else
{
list = wud.parent;
}
var timeSpan = TimeSpan.FromDays(3650);
if (maxTime.IsInteger)
{
timeSpan = TimeSpan.FromMilliseconds(maxTime.intValue());
}
else if (!maxTime.IsVar)
{
timeSpan = (TimeSpan)CastTerm(maxTime, typeof(TimeSpan));
}
DateTime expireyTime = DateTime.Now.Add(timeSpan);
while (DateTime.Now < expireyTime)
{
var results = list.WaitOne(timeSpan, out wud);
if (results == null)
{
return(exitCode.UnifyAtom("time_limit_exceeded"));
}
PlTerm copyTo = PlTerm.PlVar();
PlTermV newPlTermV = new PlTermV(testVarsCode, copyTo);
PlCall("system", "copy_term", newPlTermV);
PlTerm ctestVars = copyTo.Arg(0);
PlTerm ctestCode = copyTo.Arg(1);
PlTerm[] terms = ToTermArray(ctestVars);
int idx = terms.Length - 1;
int resdex = results.Length - 1;
while (idx >= 0 && resdex >= 0)
{
terms[idx--].FromObject(results[resdex--]);
}
try
{
if (PlCall("user", "call", new PlTermV(ctestCode)))
{
return(UnifyToProlog(PlCall(null, "=", newPlTermV), exitCode) != 0);
}
}
finally
{
list.Reset();
}
}
return(exitCode.UnifyAtom("time_limit_exceeded"));
}