<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist?]]></title><description><![CDATA[<p>Hallo!<br />
Ich mache mir die Messages-Funktion bei win32 API gerade. Ich will dass mein Programm beim Deaktivieren einfach &quot;einschläft&quot;. Wenn es wieder aktiv geworden ist, soll es &quot;aufwachen&quot;.<br />
Ich habe bemerkt, dass ich immer CPU Leistung von 90% habe, egal ob das Programm minimiert ist.<br />
Das Einschlafen soll nicht komplett sein, denn ab und zu muss irgendwie geprüft werden ob das Programm nicht wieder aktiv im Vordergrund steht.<br />
Hier ist mein Code:</p>
<pre><code class="language-cpp">while(1) {										// infinite loop
		if (PeekMessage(&amp;msg, 0, 0, 0, PM_REMOVE))	// Is there a message waiting?
			if (msg.message == WM_QUIT)				// Have we received a quit message?
				break;								// If so - quit
			else									// if not, deal with window messages
			{
				TranslateMessage(&amp;msg);				// translate the message
				DispatchMessage(&amp;msg);				// dispatch the Message
			}
		else										// If there are no messages
			if (is_active())						// Is our program active?
				if (key_pressed(VK_ESCAPE))			// Was ESC pressed?
					break;							// ESC signalled a quit
				else								// Not time to quit, update screen
				{
					// mein Programm macht etwas aufwändiges und rechnet hier
				}
//obwohl nix steht, wenn das Programm inaktiv ist, bleibt die CPU etwa 90%.
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/180703/wie-kann-man-die-cpu-belastung-verringern-wenn-das-programm-inaktiv-ist</link><generator>RSS for Node</generator><lastBuildDate>Wed, 08 Jul 2026 17:14:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/180703.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 05 May 2007 15:43:15 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 15:43:15 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich mache mir die Messages-Funktion bei win32 API gerade. Ich will dass mein Programm beim Deaktivieren einfach &quot;einschläft&quot;. Wenn es wieder aktiv geworden ist, soll es &quot;aufwachen&quot;.<br />
Ich habe bemerkt, dass ich immer CPU Leistung von 90% habe, egal ob das Programm minimiert ist.<br />
Das Einschlafen soll nicht komplett sein, denn ab und zu muss irgendwie geprüft werden ob das Programm nicht wieder aktiv im Vordergrund steht.<br />
Hier ist mein Code:</p>
<pre><code class="language-cpp">while(1) {										// infinite loop
		if (PeekMessage(&amp;msg, 0, 0, 0, PM_REMOVE))	// Is there a message waiting?
			if (msg.message == WM_QUIT)				// Have we received a quit message?
				break;								// If so - quit
			else									// if not, deal with window messages
			{
				TranslateMessage(&amp;msg);				// translate the message
				DispatchMessage(&amp;msg);				// dispatch the Message
			}
		else										// If there are no messages
			if (is_active())						// Is our program active?
				if (key_pressed(VK_ESCAPE))			// Was ESC pressed?
					break;							// ESC signalled a quit
				else								// Not time to quit, update screen
				{
					// mein Programm macht etwas aufwändiges und rechnet hier
				}
//obwohl nix steht, wenn das Programm inaktiv ist, bleibt die CPU etwa 90%.
	}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1279325</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279325</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 15:43:15 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 15:45:08 GMT]]></title><description><![CDATA[<p>Ich würde lieber unter DispatchMessage ein Sleep(0) einfügen dadurch ist die belastung immer bei 0% und das programm läuft trotzdem gleichschnell.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279327</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 15:45:08 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 15:47:12 GMT]]></title><description><![CDATA[<p>Upps, ich hab mich leider verlesen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279330</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279330</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 15:47:12 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 15:51:49 GMT]]></title><description><![CDATA[<p>Oh das Sleep(0) ist doch richtig, wenn dus als else bei is_active() einfügst:</p>
<pre><code class="language-cpp">while(1) {                                        // infinite loop
        if (PeekMessage(&amp;msg, 0, 0, 0, PM_REMOVE))    // Is there a message waiting?
            if (msg.message == WM_QUIT)                // Have we received a quit message?
                break;                                // If so - quit
            else                                    // if not, deal with window messages
            {
                TranslateMessage(&amp;msg);                // translate the message
                DispatchMessage(&amp;msg);                // dispatch the Message
            }
        else                                        // If there are no messages
            if (is_active())                        // Is our program active?
                if (key_pressed(VK_ESCAPE))            // Was ESC pressed?
                    break;                            // ESC signalled a quit
                else                                // Not time to quit, update screen
                {
                    // mein Programm macht etwas aufwändiges und rechnet hier
                }
            else
                Sleep(0); // Nun wird verlangsamt
    }
</code></pre>
<p>Sonst hast du ein unendlichen while() loop.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279331</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 15:51:49 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 15:59:21 GMT]]></title><description><![CDATA[<p>Nö, das hilft nicht. Kann das Problem sein, dass ich es als Debug Version von der Entwicklungsumgebung ausführe?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279335</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279335</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 15:59:21 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:02:26 GMT]]></title><description><![CDATA[<p>WaitMessage()</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279336</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279336</guid><dc:creator><![CDATA[j]]></dc:creator><pubDate>Sat, 05 May 2007 16:02:26 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:02:38 GMT]]></title><description><![CDATA[<p>Eigentlich nicht(außer wenn du etwa 50MHZ hättest). Der einzige Dauerloop der noch entstehen kann, ist wenn tausende Messages kommen. Dann würde das ohne Sleep(0) ablaufen. Welche windows version hast du und funktioniert das is_active() auch richtig?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279337</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279337</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 16:02:38 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:05:56 GMT]]></title><description><![CDATA[<p>Oh ja WaitMessage() wäre wirklich gut als else funktion. Dann müsste es auch funktionieren außer eben is_active() ist nicht richtig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279339</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 16:05:56 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:07:10 GMT]]></title><description><![CDATA[<p>WinXP SP2 auf 1,7GHz, 512MB RAM.</p>
<pre><code class="language-cpp">bool active_ = true;
std::bitset&lt;256&gt; keys_;

void activate() {
	active_ = true;
}

void deactivate() {
	active_ = false;
}

bool is_active() {
    return active_;
}

void set_key(WPARAM index, bool pressed) {
    keys_[index] = pressed;
}

bool key_pressed(WPARAM index) {
	return keys_[index];
}
</code></pre>
<pre><code class="language-cpp">LRESULT CALLBACK WP(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch (uMsg) {									// Check for windows messages
		case WM_ACTIVATE:							// watch for window activate message
		{
			if (!HIWORD(wParam))					// Check minimization state
				activate();			// Program is active
			else
				deactivate();		// Program is no longer active
			return 0;								// Return to the message loop
		}

		case WM_SYSCOMMAND:							// Intercept system commands
		{
			switch (wParam)	{						// Check system calls
				case SC_SCREENSAVE:					// Screensaver trying to start?
				case SC_MONITORPOWER:				// Monitor trying to enter powersave?
				return 0;							// Prevent from happening
			}
			break;									// Exit
		}

		case WM_CLOSE:								// Did we receive a close message?
		{
			PostQuitMessage(0);						// Send a quit message
			return 0;								// Jump back
		}

		case WM_KEYDOWN:							// Is a key being held down?
		{
			set_key(wParam, true);	// If so, mark it as true
			return 0;								// Jump back
		}

		case WM_KEYUP:								// Has a key been released?
		{
			set_key(wParam, false);	// If so, mark it as false
			return 0;								// Jump back
		}
	}
	// Pass all unhandled messages to DefWindowProc
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
</code></pre>
<p>Es muss korrekt sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279340</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279340</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 16:07:10 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:09:11 GMT]]></title><description><![CDATA[<p>Dennis123 schrieb:</p>
<blockquote>
<p>Oh ja WaitMessage() wäre wirklich gut als else funktion. Dann müsste es auch funktionieren außer eben is_active() ist nicht richtig.</p>
</blockquote>
<p>Super!!! Vielen Dank! Es hat PRIMA funktioniert! Sogar der verbrauchte Speicher wird beim Deaktivieren verringert! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279343</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279343</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 16:09:11 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:09:35 GMT]]></title><description><![CDATA[<p>Du hast aber kein case für WM_PAINT, oder? Das würde es eigentlich erklären. WM_PAINT muss immer behandelt werden sonst kommen glaube ich tausende WM_PAINT messages.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279344</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 16:09:35 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:11:22 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">return DefWindowProc(hwnd, uMsg, wParam, lParam);
</code></pre>
<p>Zwar ist es so, aber ich habe es noch nicht implementiert.<br />
Vielleicht kannst du mir helfen und zeigen, wie man WM_PAINT macht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279345</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279345</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 16:11:22 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:13:37 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">case WM_PAINT:
{
    PAINTSTRUCT ps;
    BeginPaint(hWnd, &amp;ps);
    // Hier kommt der zeichencode rein
    // - Also entweder die GDI funktionen oder Gdi+
    EndPaint(hWnd, &amp;ps);
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1279346</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279346</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 16:13:37 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:18:42 GMT]]></title><description><![CDATA[<p>Klasse! Noch 1 Frage! Ist es möglich die zum Zeichnen Quellcode irgendwo anders ausführen? Wie soll es dann aussehen? Und muss man explizit WM_PAINT schicken, oder?</p>
<p>Vielen Dank!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279352</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279352</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 16:18:42 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:23:39 GMT]]></title><description><![CDATA[<p>Zum einen kannst du bei WM_PAINT den gesamten zeichencode auslagern und wenn du von woanders das zeichen starten willst dann benutze InvalidateRect(hWnd, NULL, TRUE/FALSE). Der 2. Parameter kann ein Rect* sein, der 3. bestimmt, ob der hintergrund neugezeichnet werden soll(sinnvoll).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279357</guid><dc:creator><![CDATA[Dennis123]]></dc:creator><pubDate>Sat, 05 May 2007 16:23:39 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 16:24:50 GMT]]></title><description><![CDATA[<p>Ich öffne mir mal ein neues Thema.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279359</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279359</guid><dc:creator><![CDATA[khalderon]]></dc:creator><pubDate>Sat, 05 May 2007 16:24:50 GMT</pubDate></item><item><title><![CDATA[Reply to Wie kann man die CPU Belastung verringern, wenn das Programm inaktiv ist? on Sat, 05 May 2007 17:25:06 GMT]]></title><description><![CDATA[<p>Dennis123 schrieb:</p>
<blockquote>
<p>Du hast aber kein case für WM_PAINT, oder? Das würde es eigentlich erklären. WM_PAINT muss immer behandelt werden sonst kommen glaube ich tausende WM_PAINT messages.</p>
</blockquote>
<p>Nur wenn man dann nicht die DefWindowProc() aufruft, die DefWindowProc() macht sonst quasi selber ein BeginPaint()/EndPaint()</p>
<p>WM_PAINT wird solange ständig neu ans Fenster geschickt bis alle Fensterbereiche als &quot;gültig&quot; markiert sind.</p>
<p>Wenn z.B. ein Fensterbereich zum Teil überdeckt wurde, wird der ehemals überdeckte Teil als &quot;ungültig&quot; markiert und WM_PAINT geschickt. Bei BeginPaint() kann man aus der PAINTSTRUCT lesen welchen Teil man neu zeichnen soll. Beim Aufruf von EndPaint() markiert Windows diesen Teil dann wieder als gültig.</p>
<p>Mit InvalidateRect() kann man Teile auch selber als ungültig markieren. Gültig markieren kann man abgesehen von EndPaint() auch außerhalb von WM_PAINT mit ValidateRect().</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1279399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1279399</guid><dc:creator><![CDATA[geeky]]></dc:creator><pubDate>Sat, 05 May 2007 17:25:06 GMT</pubDate></item></channel></rss>