static void InteractLoop(Job job, JobInteractor jobInteractor) { // This is an example main-loop that will wait for events to happen on the remote job. bool done = false; bool finishHappened = false; do { // wait for some while to capture events from the pending job InteractiveJobEvent ev = jobInteractor.waitForEvent(TimeSpan.FromSeconds(1)); // because it is possible that events arrive in a different order in which they happened remotely, // we will wait one more time for incoming ProcedureCalls. This is done by setting 'finishHappened' // to true at the Finished event below. When we waited one time for other potential incoming messages, // after receiving the finish event, we deem this job really done. if (finishHappened) { done = true; } switch (ev) { case InteractiveJobEvent.TimeOut: // the specified amount of time (1 second) expired, do some other stuff Console.WriteLine("Waiting for events on job"); break; case InteractiveJobEvent.Error: string lastError = jobInteractor.firstIncomingError(); Console.WriteLine("error: {0}", lastError); // when we get errors, just terminate job.terminate(); done = true; break; case InteractiveJobEvent.ProcedureCall: ProcedureCall lastProcedureCall = jobInteractor.firstIncomingProcedureCall(); Console.WriteLine("procedureCall: {0}", lastProcedureCall.ProcedureName); // show what we retrieved DisplayProcedureCall(lastProcedureCall); break; case InteractiveJobEvent.Finished: Console.WriteLine("job Finished"); finishHappened = true; break; } } while (!done); }
static void AdvancedInteractLoop(Job job, JobInteractor jobInteractor) { bool done = false; bool finishHappened = false; int numberOfWaits = 0; do { // wait for some while to capture events from the pending job InteractiveJobEvent ev = jobInteractor.waitForEvent(TimeSpan.FromSeconds(1)); // because it is possible that events arrive in a different order in which they happened remotely, // we will wait one more time for incoming ProcedureCalls. This is done by setting 'finishHappened' // to true at the Finished event below. When we waited one time for other potential incoming messages, // after receiving the finish event, we deem this job really done. if (finishHappened) { done = true; } switch (ev) { case InteractiveJobEvent.TimeOut: // show some status Console.WriteLine("Waiting for events on job"); numberOfWaits++; // after waiting some time, inject a procedureCall to give us some progress updates // the AdvancedInteractProgress procedure sends (by issuing a DelegateToClient) // the desired progress updates, after that it continues where it left of the main // procedure if (numberOfWaits % 5 == 0) { Console.WriteLine("Requesting progress report"); ProcedureCall progressCall = new ProcedureCall("proc_AdvancedInteractProgress"); jobInteractor.inject(progressCall); } break; case InteractiveJobEvent.Error: string lastError = jobInteractor.firstIncomingError(); Console.WriteLine("error: {0}", lastError); // when we get errors, just terminate job.terminate(); done = true; break; case InteractiveJobEvent.ProcedureCall: ProcedureCall lastProcedureCall = jobInteractor.firstIncomingProcedureCall(); Console.WriteLine("procedureCall: {0}", lastProcedureCall.ProcedureName); // since we know we're only going to be called with one (scalar) parameter, // take some shortcuts here: Parameter p = (Parameter)lastProcedureCall.ProcedureArguments[0]; double fractionCompleted = p.Data[0].value; // let's check whether we think it's ok if (fractionCompleted > .75) { Console.WriteLine("Percentage completed = {0}%, enough, so stopping the current tast", fractionCompleted * 100.0); jobInteractor.stopTask(); // alternatively we might just stop the solver only, and continue our normal flow // for this example the solver is not running // jobInteractor.stopSolve(); } else { Console.WriteLine("Percentage completed = {0}%, not enough, continueing", fractionCompleted * 100.0); } break; case InteractiveJobEvent.Finished: Console.WriteLine("job Finished"); finishHappened = true; break; } } while (!done); }