通过命令行启动gui程序,数据处理在线程中执行,界面可暂停、终止、继续、重启

BOOL CKifDataApp::InitInstance()
{
	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.
	
	if(!AfxOleInit())//数据库初始化,此处是多余的,但是没有此处多余的初始化,程序退出时会异常
    {
        AfxMessageBox("AfxOleInit faild");
    }	
	CSASSERT_Init(KifDataLogFileName);
	CSASSERT_TIME(false, "command line : %s", ::GetCommandLine());
	CKifDataDlg dlg;
	m_pMainWnd = &dlg;
	
//	TransactCommandLine("KifData /c");//命令行处理
	TransactCommandLine(::GetCommandLine());//命令行处理

	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

int CKifDataApp::ExitInstance() 
{
	CSASSERT_TIME(false, "program exit.");
	CSASSERT_Rele();
	ConfigInfor::SaveConfigInfor();
	ConvertDataThread::ThreadClose();
	return CWinApp::ExitInstance();
}

int CKifDataApp::TransactCommandLine(LPTSTR pCommand)
{
	if(ConfigInfor::LoadConfigInfor(pCommand)
		&& !ConvertDataThread::IsRunning())
	{
		HANDLE hTemp = ::CreateThread(NULL, 0, ConvertKifDataSourceData, NULL, 0, NULL);
		ConvertDataThread::SetThreadHandle(hTemp);
	}
	return 0;
}

线程函数:


DWORD WINAPI ConvertKifDataSourceData(LPVOID pData)
{
	ConvertDataThread::SetThreadIsRunning();
	ConvertKifData::DoAll();
	::Sleep(1000);//如果时间太短,下面的消息会丢失,所以sleep一会
	::SendMessage(::AfxGetMainWnd()->GetSafeHwnd(), WM_CLOSE, 0, 0);//退出程序
	return 0;
}

多线程代码与前一文章相同:
windows多线程

猜你喜欢

转载自blog.csdn.net/weixin_43172531/article/details/103767183