Kaynağa Gözat

完成子进程运行脚本的类封装工作;

scbc.sat2 5 yıl önce
ebeveyn
işleme
0a7dd27a97

+ 1 - 1
RunPython/RunPython/Global.cpp

@@ -189,7 +189,7 @@ namespace Global
 		TCHAR* old_locale = _tcsdup(_tsetlocale(LC_CTYPE, NULL));
 		_tsetlocale(LC_CTYPE, _T("chs")); //设定中文;
 		//_ftprintf(fp, _T("%04d-%02d-%02d %02d:%02d:%02d %s\n"), gmtm.tm_year + 1990, gmtm.tm_mon + 1, gmtm.tm_mday, gmtm.tm_hour, gmtm.tm_min, gmtm.tm_sec, msg);
-		_ftprintf(fp,,msg);
+		_ftprintf(fp,msg);
 		// 关闭文件,释放资源并设置回原语言区域;
 		fclose(fp);
 		_tsetlocale(LC_CTYPE, old_locale);

+ 7 - 2
RunPython/RunPython/RunPython.cpp

@@ -42,9 +42,14 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
 		//printf("\n\n\n\n=================================================================\n\n");
 #if 1
 		CScriptExecutor excutor;
-		excutor.InitScript("E:\\bin\\ScbcCopyKey\\ScbcTest.py", "D:\\SAT\\log.txt", "", PY_RUN_TYPE::SUBPROCESS);
+		excutor.InitScript("E:\\bin\\ScbcCopyKey\\ScbcTest.py", 
+			"D:\\SAT\\log.txt", 
+			"",
+			PY_RUN_TYPE::SUBPROCESS);
 		excutor.StartScript();
-		system("pause");
+
+		while( !excutor.IsScriptOver() )
+			Sleep(100);
 #endif
 	}
 

+ 29 - 7
RunPython/RunPython/ScriptExecutor.cpp

@@ -48,6 +48,9 @@ DWORD __stdcall CScriptExecutor::_WorkerThread(LPVOID lpParam)
 		that->RunScripProcess();
 	}
 
+	CloseHandle(that->m_hWorkThread);
+	that->m_hWorkThread = NULL;
+
 	// 线程结束后,应该回调一个通知函数;
 	// ...
 
@@ -69,13 +72,6 @@ DWORD __stdcall CScriptExecutor::_LogExportThread(LPVOID lpParam)
 		CHAR chBuf[BUFSIZE] = {0};
 		BOOL bSuccess = FALSE;
 		HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
-
-		for (;;)
-		{
-			if (that->m_hStdOutRead)
-				break;
-			Sleep(100);
-		}
 		
 		do
 		{
@@ -87,6 +83,16 @@ DWORD __stdcall CScriptExecutor::_LogExportThread(LPVOID lpParam)
 			memset(chBuf, 0, BUFSIZE);
 		} while (!that->m_bStopLogExport);
 	}
+	else if ( that->m_nRunType == PY_RUN_TYPE::EMBEDDED)
+	{
+		do
+		{
+			Sleep(20);
+		} while (!that->m_bStopLogExport);
+	}
+
+	CloseHandle(that->m_hLogThread);
+	that->m_hLogThread = NULL;
 
 	return 0;
 }
@@ -541,3 +547,19 @@ void CScriptExecutor::StopScript()
 	// 结束线程;
 	EndWorkThread();
 }
+
+bool CScriptExecutor::IsScriptOver()
+{
+	if (m_nRunType == PY_RUN_TYPE::EMBEDDED)
+	{
+		if ( m_hWorkThread == NULL )
+			return true;
+	}
+	else if (m_nRunType == PY_RUN_TYPE::SUBPROCESS)
+	{
+		if ( m_pi.hProcess == NULL && m_hWorkThread == NULL )
+			return true;
+	}
+
+	return false;
+}

+ 2 - 0
RunPython/RunPython/ScriptExecutor.h

@@ -72,6 +72,8 @@ public:
 	bool StartScript();
 	// Í£Ö¹½Å±¾;
 	void StopScript();
+	// ½Å±¾ÊÇ·ñ½áÊø;
+	bool IsScriptOver();
 };
 
 #endif // __SCRIPT_EXECUTOR__