www.gusucode.com > mbc 工具箱 matlab 源码程序 > mbc/@mbcfoundation/@DCTManager/waitForState.m

    function [ok, errmsg] = waitForState(manager, job, state, stopFcn)
%WAITFORSTATE Waits for the job to return
%
%  [OK, ERRMSG] = WAITFORSTATE(MANAGER, JOB, STATE) waits for the submitted 
%  job to reach the state 'STATE', or returns an error message if there 
%  was an error.
%
%  [OK, ERRMSG] = WAITFORSTATE(MANAGER, JOB, STATE, STOPFCN) waits for the
%  submitted job to reach the state 'STATE' or for the STOPFCN to return
%  true.  When the stop function causes termination the job is cancelled,
%  which normally causes remaining tasks to not be run.  This state is not
%  treated as an error so the OK flag will be set to true and no error
%  message will be returned.
%
%  See also DISTCOMP.JOB/WAITFORSTATE.

%  Copyright 2006-2012 The MathWorks, Inc. 

errmsg = '';
timeout = 1;
ok = true;
try
    while true
        job.wait(state, timeout)
        S = job.State;
        if strcmpi(S, state)
            % If we've reached the state we're waiting for then return
            break
        elseif strcmpi(S, 'failed')
            % If the job has failed then return
            errmsg = 'Job failed.';
            ok = false;
            break
        elseif strcmpi(S, 'destroyed')
            errmsg = 'Job destroyed on scheduler.';
            ok = false;
            break
        elseif nargin >3
            if feval(stopFcn)
                % If the user has called for a stop then cancel the job and
                % return
                try
                    job.cancel;
                catch E
                end
                break
            end
        end
    end
catch ME
    errmsg = ME.message;
    ok = false;
end