ProcessMonitor::ProcessMonitor(unsigned long pid, Logger* logger){
	//Initialization
	this->logger = logger;
	this->commandLine = getCommandLineForProcessByPID(pid);
	previousActionFinished = true;
	//If command line wasn't extracted
	if (this->commandLine == NULL){
		
		std::wstringstream message;
		message << "Process ["<<pid<<"] not found. Error = " << GetLastError();		
		logMessage(message.str().c_str());

		setLastAction(LastAction::STOP);
		setProcessStatus(ProcessStatus::STOPPED);
		this->pid = 0;
		this->pHandle = NULL;
		this->tHandle = NULL;
	}
	else{
		//if command line was extracted;
		this->pid = pid;
		this->pHandle = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
		this->tHandle = NULL;//just for unification with ProcessInformation structure
		setLastAction(LastAction::START);
		setProcessStatus(ProcessStatus::IS_WORKING);
		logMessage(L"Watching process", true);
		waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread
	}
}
bool ProcessMonitor::start(){	
	//interface for starting a process
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(stop, restart) finish
	previousActionFinished = false;
	

	if (getStatus() == ProcessStatus::STOPPED){
		//start a new process only if current is stopped
		setLastAction(LastAction::START);
		if (runProc()){
			//if process was started
			setProcessStatus(ProcessStatus::IS_WORKING);//update status
			if (waitingThread.joinable()) waitingThread.join();			
			waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread
			
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working
			
			return true;//process was successfully started
		}		
	}
	
	previousActionFinished = true;
	actionFinishedCondition.notify_one();
	return false;//Process was already running or can't start a new process
}
Example #3
0
void AnimationComponent::changeAction(CCDictionary* pData)
{
	CCString* animationName=(CCString*)pData->objectForKey("name");
    int direction=((CCInteger*) pData->objectForKey("direction"))->getValue();
            
    CCAction* action=(CCAction*)actionForName(animationName->getCString(),direction);
    if(action && m_lastAction!=action){
        CCNode* owner=(CCNode*) m_pOwner;
        owner->stopAction(m_lastAction);
        owner->runAction(action);
        setLastAction(action);
    }else {
        CCLOG("unknow animation name %s",animationName);
    }
}
bool ProcessMonitor::restart(){
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(start, stort) finish
	previousActionFinished = false;

	if (getStatus() == ProcessStatus::IS_WORKING){
		//restart process only if it's running
		setLastAction(LastAction::RESTART);
		setProcessStatus(ProcessStatus::RESTARTING);//update status
		if (restartProc()){
			//if process was successfully restarted
			setProcessStatus(ProcessStatus::IS_WORKING);//update status
			if(waitingThread.joinable()) waitingThread.join();//wait for previous call of waitFunction to finish
			waitingThread = std::thread(&ProcessMonitor::waitForProcess, this);//wait for process closing in another thread			
				
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working
			
			return true;
		}
		else{
			//if something gone wrong
			DWORD exitCode;
			if (GetExitCodeProcess(pi.hProcess, &exitCode)){
				//checking exit code
				if (exitCode == 259) setProcessStatus(ProcessStatus::IS_WORKING);//259==RUNNING
				else setProcessStatus(ProcessStatus::STOPPED);//any another exit code indicate crash or closing
			}
			else{
				//can't get exit code
				setProcessStatus(ProcessStatus::STOPPED);
			}
		}				
	}

	previousActionFinished = true;
	actionFinishedCondition.notify_one();//next action can start working
	return false;//Process wasn't restarted or process wasn't restarted correct
}
bool ProcessMonitor::stop(){
	//interface for stopping a process
	std::unique_lock <std::mutex> uniqueOperationLock(operationMutex);
	while (!previousActionFinished) actionFinishedCondition.wait(uniqueOperationLock);//wait until another action(start, restart) finish
	previousActionFinished = false;
	
	if (getStatus() == ProcessStatus::IS_WORKING){
		//stop process only if it's running
		setLastAction(LastAction::STOP);
		if (stopProc()){
			//if process was successfully stopped
			setProcessStatus(ProcessStatus::STOPPED);//update status
			previousActionFinished = true;
			actionFinishedCondition.notify_one();//next action can start working

			return true;//process was successfuly stopped
		}
	}

	previousActionFinished = true;
	actionFinishedCondition.notify_one();//next action can start working
	return false;//process was not running or can't stop process
}