Jump to content

C++ problem - High idle CPU usage

colonel_mortis
Go to solution Solved by colonel_mortis,

Oh well. It looks like it was an issues with the WndProc because now it's been rewritten, it works. Don't know what though.

I have found a work around to the original problem, but if you can suggest a better method, that would be appreciated. On the 3rd post in the thread, I have a new problem regarding high idle (and usage) CPU usage.

 

I have a program which works as a score board in Win32 native code. This is the extract I'm having problems with:

#define IDC_NAME_ENTRY 200#define IDC_CONTACT_ENTRY 201#define IDC_SCORE_ENTRY 202#define IDC_TICKET_ENTRY 203#define IDC_ENTER_BUTTON 210class scorestr{public:	LPWSTR name;	LPWSTR contact;	int score;	bool init;	int ticket;};scorestr* score= new scorestr[300];HWND hEditName=0, hEditContact=0, hEditScore=0, hEditTicket=0, hButtonEnter=0;HFONT hfTitle=0, hfMain=0, hfCaption=0, hfInput=0;WCHAR bufName [100], bufContact[300];int bufScore, bufTicket;
case IDC_ENTER_BUTTON:			{				GetDlgItemText(hwnd, IDC_NAME_ENTRY, bufName, 100);				GetDlgItemText(hwnd, IDC_CONTACT_ENTRY, bufContact, 300);				bufScore=GetDlgItemInt(hwnd, IDC_SCORE_ENTRY, FALSE, TRUE);				bufTicket=GetDlgItemInt(hwnd, IDC_TICKET_ENTRY, FALSE, TRUE);				if(bufName[0]=='\0'){					MessageBox(hwnd, L"You must enter a name", L"Oops", MB_ICONINFORMATION|MB_OK);					break;				}				for(int l=0;l<300;l++){					if(bufScore>score[l].score){						for(int m=299;m>=l;m--){							if(score[m].init==true){								score[m+1].name=score[m].name;								score[m+1].contact=score[m].contact;								score[m+1].init=score[m].init;								score[m+1].ticket=score[m].ticket;								score[m+1].score=score[m].score;							}						}						score[l].name=bufName;						score[l].contact=bufContact;						score[l].score=bufScore;						score[l].ticket=bufTicket;						score[l].init=true;						RedrawWindow(hwnd, NULL, NULL, RDW_ALLCHILDREN|RDW_VALIDATE|RDW_INVALIDATE|RDW_ERASE);						WCHAR buffer[35];						swprintf_s(buffer, 35, L"You are position number: %d", l+1);						MessageBox(hwnd, buffer, L"Congratulations!", MB_ICONINFORMATION|MB_OK);						break;					}				}				SetDlgItemText(hwnd, IDC_CONTACT_ENTRY, L"");				SetDlgItemText(hwnd, IDC_NAME_ENTRY, L"");				SetDlgItemText(hwnd, IDC_SCORE_ENTRY, L"");				SetDlgItemText(hwnd, IDC_TICKET_ENTRY, L"");			}			break;

Basically, when you press the button, it reads the text boxes, checks to see where is is in the ordered array of previous entries, pushes back the entries after the new score and enters it. This all works fine, but because it seems to be giving a pointer to the entry rather than the actual text, when I put in a new entry, it changes all of the names and descriptions, rather than just the one the new one.

Images:

Hai7rmz.png

Changes to:

uaKXwNQ.png

Is there any way that I can transfer the contents of the variable to the new variable without affecting the others. I'm sure it's really simple, but I can't think of a solution right now.

Thanks in advance (and sorry for the length of the post)

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I've fixed it for now by making another array to enter into, but it's not optimum, and could potentially run out. If anybody has a better suggestion, I would love to hear it.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I've kinda sorted that, but if anyone can suggest a better method that previously mentioned, I would appreciate hearing it.

Now, I have a new, really strange problem - when I run the secondary window of my program, either as another window of the main process or as a secondary window, the CPU usage is at 25% (basically it's using 100% of 1 core on my i5 according to process hacker) despite the fact that it'd doing nothing, and MessageBox doesn't work - the sound plays and the window disables, but no box appears. The issue does not occur if I comment out ShowWindow(hwnd, SW_SHOW); even though it would still do the processing (presumably...?)

The code for the process is here:

Spoiler

#include <Windows.h>#include <string.h>class scorestr{public:	LPWSTR name;	LPWSTR contact;	int score;	bool init;	int ticket;};scorestr* score= new scorestr[3];LRESULT CALLBACK SuppWndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow){	WNDCLASSEX wcSupp;	HWND hSupp;	MSG msg;	wcSupp.cbSize		=sizeof(WNDCLASSEX);	wcSupp.style		=0;	wcSupp.lpfnWndProc	=SuppWndProc;	wcSupp.cbClsExtra	=0;	wcSupp.cbWndExtra	=0;	wcSupp.hInstance	=hInstance;	wcSupp.hIcon		=LoadIcon(NULL, IDI_APPLICATION);	wcSupp.hCursor		=LoadCursor(NULL, IDC_ARROW);	wcSupp.hbrBackground=(HBRUSH)(COLOR_WINDOW);	wcSupp.lpszMenuName	=NULL;	wcSupp.lpszClassName=L"Top10SuppWindowClass";	wcSupp.hIconSm		=LoadIcon(NULL, IDI_APPLICATION);	if(!RegisterClassEx(&wcSupp)){		MessageBox(NULL, L"Failed to register the window class.\nThe application will now exit", L"Critcal Error", MB_ICONERROR|MB_OK);		return 1;	}	hSupp=CreateWindowEx(WS_EX_CLIENTEDGE, L"Top10SuppWindowClass", L"Top10 scoring system (top 3) \x00a9 colonel_mortis 2013", WS_OVERLAPPEDWINDOW,		0,0,800,300,NULL, NULL, hInstance, NULL);	if(!hSupp){		MessageBox(NULL, L"Could not create window\nThe application will now exit", L"Critical Error", MB_ICONERROR|MB_OK);		return 1;	}	ShowWindow(hSupp, nCmdShow);	UpdateWindow(hSupp);	while(GetMessage(&msg, NULL, 0,0)>0){		TranslateMessage(&msg);		DispatchMessage(&msg);	}	return msg.wParam;}LRESULT CALLBACK SuppWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam){	switch(msg){	case WM_CLOSE:		DestroyWindow(hwnd);		break;	case WM_DESTROY:		{			//Destroy the objects here				//end of destruction spree			PostQuitMessage(0);		}		break;	case WM_CREATE:		{			//Create some objects here			HICON hIcon = (HICON)LoadImage(NULL, L"top10.ico", IMAGE_ICON, 32, 32, LR_LOADFROMFILE);			if(hIcon){				SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);			}			else{				MessageBox(NULL, L"Could not load icon. This isn't good\n(small window)", L"Error", MB_ICONEXCLAMATION|MB_OK);			}			//end of playing God		}		break;	case WM_PAINT:		{			//How are your art skills?		}		break;	case WM_COMMAND:		switch (LOWORD(wParam))		{		default:			//MessageBox(NULL, L"I don't know what you're talking about\nStop pressing random buttons", L"Oh dear...", MB_ICONEXCLAMATION|MB_OK);			return DefWindowProc(hwnd, msg, wParam, lParam);		}		break;			case WM_COPYDATA: //Before anybody thinks to blame it on this bit, it occurs even without it		{			//Receive the data from the other thread			PCOPYDATASTRUCT cds=(PCOPYDATASTRUCT)lParam;			memcpy(&score[cds->dwData],cds->lpData, cds->cbData);		}		break;	default:		return DefWindowProc(hwnd, msg, wParam, lParam);	}	return TRUE;}

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Oh well. It looks like it was an issues with the WndProc because now it's been rewritten, it works. Don't know what though.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×