本文介绍了如何减少VC ++中的CPU使用率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在VC ++ 2005中开发了一个屏幕截图应用程序.它将获取屏幕截图并将其保存在服务器MYSQL数据库中.过程是,最初,文件将保存在C:驱动器中,然后在获取Blob数据之后,将删除该文件.但是它已经安装在我办公室的几乎所有系统中.问题是,它占用超过65%的CPU使用率.如何避免这种情况.请帮我. AFX_ODBC_CALL(:: SQLExecute(m hstmt)花费的时间太长了......

I have developed a screenshot application in VC++ 2005. It will take screenshots and save it in the server MYSQL database. The process is that, initially the file will be saving in C: drive, then after taking the blob data, the file will be removed. But it has been installed almost all system in my office. The problem is that, it takes more than 65% CPU usage. How to avoid this. Please help me. AFX_ODBC_CALL(::SQLExecute(m hstmt) takes too long......

void gdiscreen()
{
	using namespace Gdiplus;
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	int c=0;
	HDC scrdc, memdc;
	HBITMAP membit;
	HBITMAP hOldBitmap;
	CString lpfilename;
	char buffer[1000];

	for(;;)
	{
		scrdc = ::GetDC(0);
		int Height = GetSystemMetrics(SM_CYSCREEN);
		int Width = GetSystemMetrics(SM_CXSCREEN);
		memdc = CreateCompatibleDC(scrdc);
		membit = CreateCompatibleBitmap(scrdc, Width, Height);
		hOldBitmap=(HBITMAP) SelectObject(memdc, membit);
		BitBlt(memdc, 0, 0, Width, Height, scrdc, 0, 0, SRCCOPY);
		//Gdiplus::Bitmap bitmap(membit, NULL);
		Gdiplus::Bitmap* bitmap =  ::new Bitmap(membit, NULL );

		CLSID clsid;
		GetEncoderClsid(L"image/jpeg", &clsid);
		sprintf_s(buffer,"C:\\Program Files\\image%u.jpeg",c);
		lpfilename=buffer;
		bitmap->Save(lpfilename, &clsid);
		IStream* tmpbuf = NULL;
		CreateStreamOnHGlobal(NULL, true, &tmpbuf);
		bitmap->Save(tmpbuf, &clsid);
		HGLOBAL hg = NULL;
		HRESULT  hr = GetHGlobalFromStream(tmpbuf, &hg);
		SIZE_T uSize = GlobalSize(hg);
		//GlobalUnlock(hg);


		int len=(int)uSize;
		CFile file;
		BYTE buf[150];
		CStringA charstr(lpfilename);
		CString strblob;
	   const char *szSingle;
       szSingle=((const char *) charstr);

		CString strDateTime;
	   SYSTEMTIME datetimenew;
			::GetLocalTime(&datetimenew);
			strDateTime.Format(_T("%02i-%02i-%02i %d:%d:%d"),
								datetimenew.wYear,
								datetimenew.wMonth,
								datetimenew.wDay,
								datetimenew.wHour,
								datetimenew.wMinute,
								datetimenew.wSecond);

		CdbImages       dbImages(&theApp.m_DB);

		dbImages.Open();
        dbImages.AddNew();

        CFile		fileImage;
        CFileStatus	fileStatus;

        fileImage.Open(lpfilename, CFile::modeRead);
        fileImage.GetStatus(fileStatus);

		dbImages.m_dateTime = strDateTime;
        //dbImages.m_BLOBName = fileImage.GetFileTitle();
		dbImages.m_BLOBName = SqlStr3;

        dbImages.m_BLOBImage.m_dwDataLength = fileStatus.m_size;

        HGLOBAL hGlobal		= GlobalAlloc(GPTR,fileStatus.m_size);
        dbImages.m_BLOBImage.m_hData = GlobalLock(hGlobal);

        fileImage.Read(dbImages.m_BLOBImage.m_hData,fileStatus.m_size);
		dbImages.SetFieldDirty(&dbImages.m_BLOBImage);
        dbImages.SetFieldNull(&dbImages.m_BLOBImage,FALSE);
		dbImages.m_log_user = loguser;
        dbImages.Update();
		fileImage.Close();
		 dbImages.Close();
		 GlobalUnlock(hGlobal);

		::delete bitmap;
		bitmap = NULL;
		DeleteObject(memdc);
		DeleteObject(membit);
		::ReleaseDC(0,scrdc);
		CFile::Remove(lpfilename);


		 //Sleep(10000);
		 int iSecret, iRandom;
		 iSecret = rand() % 20 + 1;
		iRandom=iSecret*60000;
		Sleep(iRandom);
	    c++;
		}

		GdiplusShutdown(gdiplusToken);
}

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	using namespace Gdiplus;
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;
	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}
	}

	free(pImageCodecInfo);
	return 0;
}

推荐答案




这篇关于如何减少VC ++中的CPU使用率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:27
查看更多