文件监控(一) 处理结构(pthread)

文件监控结构(pthread)

//①开始监控目录
//  将监控到的文件放入list
void* ThreadWatcher(void* pParam)
{
	dzlog_notice("[线程开启]开始监控目录 ThreadWatcher PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	Cwx_kk_upDlg * dlg = (Cwx_kk_upDlg*)pParam;
	
	while(true){
		if(m_ExitCode==1)
			goto end;
		Sleep(1);
	}

end:
	sprintf(errorInfo, "[线程退出]开始监控目录 ThreadWatcher  PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	dzlog_notice(errorInfo);
	pthread_exit(errorInfo);
	return 0;
}


//②处理监控到的数据
//  获取车牌等信息、检查数据库中是否存在该图片,若存在则继续处理下一个,若不存在则复制图片从ftp到http,写入数据库,删除ftp下的文件
void* ThreadProcessWatchedFiles(void* pParam)
{
	dzlog_notice("[线程开启]处理监控到的数据 ThreadProcessWatchedFiles  PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	Cwx_kk_upDlg * dlg = (Cwx_kk_upDlg*)pParam;

	while(true){
		if(m_ExitCode==1)
			goto end;
		Sleep(1);
	}
end:
	sprintf(errorInfo, "[线程退出]处理监控到的数据 ThreadProcessWatchedFiles PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	dzlog_notice(errorInfo);
	pthread_exit(errorInfo);
	return 0;
}


//③上传数据
//  读取数据库,获取未上传的图片进行上传,如果http下的图片文件不存在,则读取ftp下的图片并进行上传
void* ThreadUpload(void* pParam)
{
	dzlog_notice("[线程开启]上传数据 ThreadUpload  PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	Cwx_kk_upDlg * dlg = (Cwx_kk_upDlg*)pParam;

	while(true){
		if(m_ExitCode==1)
			goto end;
		Sleep(1);
	}
end:
	sprintf(errorInfo, "[线程退出]上传数据 ThreadUpload  PID : 0x%x  GetLastError(%0x%x)", pthread_self(), GetLastError());
	dzlog_notice(errorInfo);
	pthread_exit(errorInfo);
	return 0;
}



pthread_t thread1, thread2, thread3;  
void*	pth_join_ret;



//开始工作线程
void * ThreadStartAll(void *pParam)
{
	threadRuning = !threadRuning;

	Cwx_kk_upDlg * dlg = (Cwx_kk_upDlg*)pParam;
	CMenu *pMenu = AfxGetApp()->m_pMainWnd->GetMenu(); 
	CMenu *pSubMenu = pMenu->GetSubMenu(2);

	dzlog_notice("======================================================================");
	if(threadRuning){
		dzlog_notice("开始工作线程 StartWatch");
		m_ExitCode = 0;
		pthread_create(&thread1, NULL, ThreadWatcher, pParam); //开启监控目录线程
		pthread_create(&thread2, NULL, ThreadProcessWatchedFiles, pParam); //开启处理监控到的数据处理线程
		pthread_create(&thread3, NULL, ThreadUpload, pParam); //开启数据上传线程

		//AfxBeginThread(ThreadWatcher, this);//开启监控目录线程
		//AfxBeginThread(ThreadProcessWatchedFiles, this);//开启处理监控到的数据处理线程
		//AfxBeginThread(ThreadUpload, this);//开启数据上传线程
		pSubMenu->ModifyMenu(ID_START_WATCH, MF_BYCOMMAND ,ID_START_WATCH,"关闭监控");
	}else{
		dzlog_notice("关闭工作线程 OnStartWatch");
		m_ExitCode = 1;
		pthread_join( thread1, &pth_join_ret);
		dzlog_notice("关闭线程 thread1[监控目录线程]   retruns : %s ", pth_join_ret);
		pthread_join( thread2, &pth_join_ret);
		dzlog_notice("关闭线程 thread2[数据处理线程]   retruns : %s ", pth_join_ret);
		pthread_join( thread3, &pth_join_ret);
		dzlog_notice("关闭线程 thread3[数据上传线程]   retruns : %s ", pth_join_ret);

	//	
		pSubMenu->ModifyMenu(ID_START_WATCH, MF_BYCOMMAND ,ID_START_WATCH,"开启监控");
	}
	sprintf(errorInfo, "[线程退出] 开始工作线程 ThreadStartAll  PID : 0x%x ", pthread_self() );
	pthread_exit(errorInfo);

	return 0;
}


//开始监控目录
void Cwx_kk_upDlg::OnStartWatch()
{
	pthread_t thread;
	pthread_create(&thread, NULL, ThreadStartAll, this); //开启线程
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值