如何减少VC ++中的CPU使用率? [英] How to reduce CPU Usage in VC++?

查看:129
本文介绍了如何减少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;
}

推荐答案

您可能有一个尽可能快的循环运行,可以通过添加某种步调来避免这种情况.例如,如果您正在拍摄屏幕截图并将其尽快发送到数据库,则您的循环可能会占用大量CPU时间.为了提高效率,您希望有一个可设置的时间间隔,以适应此操作时间.当然,没有任何代码,这只是对使用大量CPU时间的猜测.
You probably have a loop running as fast as it can, you can avoid this by adding some sort of pacing. For example, if you''re taking screen shots and sending them out to a database as fast as possible, its possible that your loop is taking up a large amount of CPU time. For efficiency, you''d want to have a settable time interval that would pace this time of operation. Of course, without any code, its just a guess as to what''s using so much CPU time.


停止编写程序,尤其是那些监视您同事的程序.它将释放大量的CPU.

—SA
Stop writing programs, especially those spying on your colleagues. It will free up a good deal of CPU.

—SA


根据您的简短说明,很难弄清楚您在做什么.您多久捕获一次这些屏幕截图?

您是否考虑过在归档之前使用JPEG格式压缩图像?
From your short explanation, it is really hard to figure out what you are doing. How often do you capture those screenshots ?

Have you considered compressing the image before archival, using the JPEG format for instance ?


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆