Click here to Skip to main content
15,895,746 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How to extract a string between two delimiters ? Pin
Jijo.Raj28-Aug-08 19:22
Jijo.Raj28-Aug-08 19:22 
AnswerRe: How to extract a string between two delimiters ? Pin
sashoalm28-Aug-08 21:22
sashoalm28-Aug-08 21:22 
QuestionHow to send a 'string' to a port ? Pin
AprNgp28-Aug-08 18:51
AprNgp28-Aug-08 18:51 
AnswerRe: How to send a 'string' to a port ? Pin
Rajesh R Subramanian28-Aug-08 19:24
professionalRajesh R Subramanian28-Aug-08 19:24 
GeneralRe: How to send a 'string' to a port ? Pin
AprNgp28-Aug-08 19:29
AprNgp28-Aug-08 19:29 
GeneralRe: How to send a 'string' to a port ? Pin
AprNgp28-Aug-08 19:35
AprNgp28-Aug-08 19:35 
GeneralRe: How to send a 'string' to a port ? Pin
Rajesh R Subramanian28-Aug-08 19:48
professionalRajesh R Subramanian28-Aug-08 19:48 
QuestionCreateThread Function question Pin
monsieur_jj28-Aug-08 16:16
monsieur_jj28-Aug-08 16:16 
Hi all,

I have this project which is downloading product updates over the internet. What I do first is that before I can go to the second download the first download must be finished. Now I have decided to create a thread so I can download updates simultaneously.

Let me show a bit of the code:

while (m_bIsRunning)
	{
		Sleep(5000);			
			if (isUpdateTime())
			{
			CSiteCode m_CSiteCode;
				RMproduct* MPoint = new RMproduct(L"ManagePoint",ManagepointService, ManagepointVersion);
				if (isPrdInstalled(MPoint))
				{
					GetVersion(MPoint);
					MPoint->m_SiteCode = m_CSiteCode.GetMPSiteCode(MPoint->m_SerialNo.c_str());
					dloadAvailable = DoCheckForProductUpdates(MPoint);
					if(dloadAvailable)
					{
					GetUpdate(MPoint);
					}
				}
				delete MPoint;

				RMproduct* ReRite = new RMproduct(L"ReRite",ReriteService, ReriteVersion);
				if (isPrdInstalled(ReRite))
				{
					GetVersion(ReRite);
					ReRite->m_SiteCode = m_CSiteCode.GetReRiteSiteCode(ReRite->m_SerialNo.c_str());
					dloadAvailable = DoCheckForProductUpdates(ReRite);
					if(dloadAvailable)
					{
					GetUpdate(ReRite);
					}
				}
				delete ReRite;

				RMproduct* ReRiteAsian = new RMproduct(L"ReRite",ReriteAsianService, ReriteAsianVersion);
				if (isPrdInstalled(ReRiteAsian))
				{
					GetVersion(ReRiteAsian);
					ReRiteAsian->m_SiteCode = m_CSiteCode.GetReRiteAsianSiteCode(ReRiteAsian->m_SerialNo.c_str());
					dloadAvailable= DoCheckForProductUpdates(ReRiteAsian);
				if(dloadAvailable)
					{
					GetUpdate(MPoint);
					}
				}
				delete ReRiteAsian;

			}
		
	}


This is service btw. Now the function GetUpdate call a function called downloadfile:

bool CUpdateServiceModule::DownloadFile(RMUpdates* prd)
{
	bool result = false;
	//to do create a thread for each download

	int Data_Of_Thread_1 = 1;            // Data of Thread 1
	HANDLE Handle_Of_Thread_1 = 0;       // variable to hold handle of Thread 1
	HANDLE Array_Of_Thread_Handles[3];   // Aray to store thread handles

	Handle_Of_Thread_1 = CreateThread( NULL, 0, Thread_no_1, ((void*)prd), 0, NULL);  
				if ( Handle_Of_Thread_1 == NULL)  ExitProcess(Data_Of_Thread_1);

	// Store Thread handles in Array of Thread Handles as per the requirement of WaitForMultipleObjects() 
	Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;

	// Wait until all threads have terminated.
	WaitForMultipleObjects( 3, Array_Of_Thread_Handles, TRUE, INFINITE);
	//printf("Since All threads executed lets close their handles \n");
	// Close all thread handles upon completion.
    CloseHandle(Handle_Of_Thread_1);

	//DownloadThread((void*)prd);
	Sleep( 100 );
	return result;
}


This download file is creating a thread that calls the downloadthread global function which downloads the update. The behavior of this downloadthread function is that even the service stops once it is restored it will start the download where it ended:

DWORD  WINAPI Thread_no_1( PVOID PrdPtr ) 
		{
			DownloadThread((void*)PrdPtr);
			return 0; 
		}

void DownloadThread(PVOID PrdPtr)
{
	RMUpdates *ParamPtr;
	ParamPtr = (RMUpdates *) PrdPtr;

	FILE * pFile = NULL;
	
	DWORD dwPacketSize =5;

	PBYTE  pBuffer  = new BYTE[dwPacketSize * 1024];

	double dOffsetToSeek =0;
	
	std::wstring strTmpFileName = ParamPtr->savePath.c_str();
	strTmpFileName.append(L".tmp");

	std::wstring strFileNameAtLocalMachine= ParamPtr->savePath.c_str();

	int res = DeleteFile(strFileNameAtLocalMachine.c_str());
	
	double dCurrentFileSize;

	if(IfLocalFileExist(strTmpFileName.c_str(), &dCurrentFileSize)) 
	{
		dOffsetToSeek = dCurrentFileSize;
	}
	
	CString strRangeQuest;

	if(dCurrentFileSize>0){   
		strRangeQuest.Format( _T("%sRange: bytes=%d-\r\n"), szHeaders,static_cast<ULONGLONG>(dCurrentFileSize));
	}
	else        
		strRangeQuest = szHeaders;

	if (!(pFile = _wfopen (strTmpFileName.c_str(), L"a+b" ) ) )
	{		
		return;
	}
	//CUpdateServiceModule UpdateService;
	try
	{ 	
	CInternetSession Session(_T("UpdateService"));

	CHttpConnection Connection1(Session,ParamPtr->link.c_str());

	BYTE Buffer[4096];

	CHttpFile dFile(Session,ParamPtr->link.c_str());
	CInternetFile::CInfo Info(dFile); //test
	dFile.SendRequest(strRangeQuest);
	DWORD dwRead;	

	for (dwRead = 1 ; dwRead; )	
	{
		dFile.Read(Buffer,sizeof(Buffer),dwRead);
		if (dwRead>0)
			fwrite(Buffer, sizeof (char), dwRead , pFile);		
	}
	fclose (pFile);
	BOOL moved = MoveFile(strTmpFileName.c_str(), strFileNameAtLocalMachine.c_str());	
	
	if(moved)
	{
		EmailUser(strFileNameAtLocalMachine.c_str());
		//UpdateService.WriteLogFile(TRUE, UPDATEMGR, _T(" has downloaded "), ParamPtr->prdName.c_str(), _T(""), _T(""), _T("WillNotBeInErrRptEmail"));
	}
	DeleteFile(strTmpFileName.c_str());
	


	//set status to 1	
	HKEY hkey;
	TCHAR szKey[256];	
	int nValue = 0;


		szKey[0] = _T('\0');							

		_tcscpy(szKey, ServiceRegEntry);
		_tcscat(szKey, UPDATEMGR);
		_tcscat(szKey, ServiceParam);
		_tcscat(szKey, _T("\\"));
		_tcscat(szKey,ParamPtr->prdName.c_str());
		_tcscat(szKey, _T("\\"));
		_tcscat(szKey,ParamPtr->nameVer.c_str());						

		if (RMRegOpenKeyEx(HKEY_LOCAL_MACHINE,szKey,0,KEY_QUERY_VALUE | KEY_SET_VALUE,&hkey) == ERROR_SUCCESS)			
		{	
			nValue = 1; //finished downloading
			RMRegSetValueEx(hkey,UPDATESTAT,0,REG_DWORD,(BYTE*)&nValue,sizeof(nValue));					
			RMRegCloseKey(hkey);
		}
	}
	catch(CInternetException& err)
	{
		err.GetErrorMessage();
		//UpdateService.WriteLogFile(TRUE, UPDATEMGR, _T(" : "), err.GetErrorMessage(), _T(""), _T(""), _T("WillNotBeInErrRptEmail"));
	}
}


Now what I would like to know is that while the first thread is downloading the first update will it go to the next update and download it? Or still my code will just wait for the first thread to download the first update? I am having a hard time debugging this as I dont know how the process splits.

Thanks,
Jayjay
AnswerRe: CreateThread Function question Pin
Jijo.Raj28-Aug-08 18:15
Jijo.Raj28-Aug-08 18:15 
GeneralRe: CreateThread Function question Pin
monsieur_jj28-Aug-08 20:07
monsieur_jj28-Aug-08 20:07 
QuestionRe: CreateThread Function question Pin
Mark Salsbery29-Aug-08 8:59
Mark Salsbery29-Aug-08 8:59 
AnswerRe: CreateThread Function question Pin
monsieur_jj31-Aug-08 16:23
monsieur_jj31-Aug-08 16:23 
GeneralRe: CreateThread Function question Pin
Mark Salsbery1-Sep-08 7:35
Mark Salsbery1-Sep-08 7:35 
GeneralRe: CreateThread Function question Pin
monsieur_jj1-Sep-08 14:17
monsieur_jj1-Sep-08 14:17 
QuestionHtmlHelp v x64 Pin
Chris Losinger28-Aug-08 14:12
professionalChris Losinger28-Aug-08 14:12 
Questionproblem with RegEnumValue to read the values in the registry.. Pin
hariakuthota28-Aug-08 12:23
hariakuthota28-Aug-08 12:23 
QuestionRe: problem with RegEnumValue to read the values in the registry.. Pin
David Crow28-Aug-08 15:36
David Crow28-Aug-08 15:36 
AnswerRe: problem with RegEnumValue to read the values in the registry.. Pin
Jijo.Raj28-Aug-08 18:24
Jijo.Raj28-Aug-08 18:24 
AnswerRe: problem with RegEnumValue to read the values in the registry.. Pin
krmed29-Aug-08 0:48
krmed29-Aug-08 0:48 
QuestionHow to have use different version resources for different configurations Pin
Wheatbread28-Aug-08 6:06
Wheatbread28-Aug-08 6:06 
AnswerRe: How to have use different version resources for different configurations Pin
enhzflep28-Aug-08 7:29
enhzflep28-Aug-08 7:29 
AnswerRe: How to have use different version resources for different configurations Pin
Michael Dunn29-Aug-08 13:12
sitebuilderMichael Dunn29-Aug-08 13:12 
Questionactivex - internet exploret has blocked my site because of an unsafe manner Pin
simon alec smith28-Aug-08 5:13
simon alec smith28-Aug-08 5:13 
Questionhow to override start button? Pin
dj440028-Aug-08 3:51
dj440028-Aug-08 3:51 
AnswerRe: how to override start button? Pin
Perspx28-Aug-08 4:01
Perspx28-Aug-08 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.