<?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[Problem mit Parameterübergabe an Thread]]></title><description><![CDATA[<p>Leider funktioniert das von mir modifizierte Beispiel aus der MSDN<br />
(wer Visual <a href="http://c++.Net" rel="nofollow">c++.Net</a> Installiert hat <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1031/dllproc/base/creating_threads.htm" rel="nofollow">ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1031/dllproc/base/creating_threads.htm</a> ) nicht.</p>
<p>Es sollte eigentlich 60 ausgegeben werden es kommt aber 1244800.<br />
Wo liegt bloss das Problem.</p>
<p>Hier der Quellcode:</p>
<p>[cpp]<br />
#include &lt;windows.h&gt;<br />
#include &lt;conio.h&gt;</p>
<p>DWORD WINAPI ThreadFunc( LPVOID lpParam )<br />
{<br />
char szMsg[80];</p>
<p>wsprintf( szMsg, &quot;Parameter = %d.&quot;, <em>(DWORD</em>)lpParam );<br />
MessageBox( NULL, szMsg, &quot;ThreadFunc&quot;, MB_OK );</p>
<p>return 0;<br />
}</p>
<p>HANDLE Start_Window_Observer()<br />
{<br />
DWORD dwThreadId, <strong>dwThrdParam = 60;</strong><br />
HANDLE hThread;</p>
<p>hThread = CreateThread(<br />
NULL, // default security attributes<br />
0, // use default stack size<br />
ThreadFunc, // thread function<br />
&amp;dwThrdParam, // argument to thread function<br />
0, // use default creation flags<br />
&amp;dwThreadId); // returns the thread identifier</p>
<p>SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);<br />
return hThread;<br />
}</p>
<p>HANDLE Stop_Window_Observer(HANDLE hThread)<br />
{<br />
TerminateThread(hThread, 0);<br />
CloseHandle(hThread);<br />
hThread = NULL;<br />
return hThread;<br />
}</p>
<p>int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
HANDLE hThread;<br />
printf(&quot;Window Observer gestartet\n\n&quot;);<br />
hThread = Start_Window_Observer();<br />
while(getchar() != '\n');<br />
hThread = Stop_Window_Observer(hThread);<br />
return 0;<br />
}<br />
[/cpp]</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/85300/problem-mit-parameterübergabe-an-thread</link><generator>RSS for Node</generator><lastBuildDate>Sun, 12 Apr 2026 02:04:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/85300.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 07 Sep 2004 14:46:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit Parameterübergabe an Thread on Tue, 07 Sep 2004 14:47:34 GMT]]></title><description><![CDATA[<p>Leider funktioniert das von mir modifizierte Beispiel aus der MSDN<br />
(wer Visual <a href="http://c++.Net" rel="nofollow">c++.Net</a> Installiert hat <a href="ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1031/dllproc/base/creating_threads.htm" rel="nofollow">ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1031/dllproc/base/creating_threads.htm</a> ) nicht.</p>
<p>Es sollte eigentlich 60 ausgegeben werden es kommt aber 1244800.<br />
Wo liegt bloss das Problem.</p>
<p>Hier der Quellcode:</p>
<p>[cpp]<br />
#include &lt;windows.h&gt;<br />
#include &lt;conio.h&gt;</p>
<p>DWORD WINAPI ThreadFunc( LPVOID lpParam )<br />
{<br />
char szMsg[80];</p>
<p>wsprintf( szMsg, &quot;Parameter = %d.&quot;, <em>(DWORD</em>)lpParam );<br />
MessageBox( NULL, szMsg, &quot;ThreadFunc&quot;, MB_OK );</p>
<p>return 0;<br />
}</p>
<p>HANDLE Start_Window_Observer()<br />
{<br />
DWORD dwThreadId, <strong>dwThrdParam = 60;</strong><br />
HANDLE hThread;</p>
<p>hThread = CreateThread(<br />
NULL, // default security attributes<br />
0, // use default stack size<br />
ThreadFunc, // thread function<br />
&amp;dwThrdParam, // argument to thread function<br />
0, // use default creation flags<br />
&amp;dwThreadId); // returns the thread identifier</p>
<p>SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL);<br />
return hThread;<br />
}</p>
<p>HANDLE Stop_Window_Observer(HANDLE hThread)<br />
{<br />
TerminateThread(hThread, 0);<br />
CloseHandle(hThread);<br />
hThread = NULL;<br />
return hThread;<br />
}</p>
<p>int _tmain(int argc, _TCHAR* argv[])<br />
{<br />
HANDLE hThread;<br />
printf(&quot;Window Observer gestartet\n\n&quot;);<br />
hThread = Start_Window_Observer();<br />
while(getchar() != '\n');<br />
hThread = Stop_Window_Observer(hThread);<br />
return 0;<br />
}<br />
[/cpp]</p>
]]></description><link>https://www.c-plusplus.net/forum/post/601730</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/601730</guid><dc:creator><![CDATA[MasterGandi]]></dc:creator><pubDate>Tue, 07 Sep 2004 14:47:34 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit Parameterübergabe an Thread on Tue, 07 Sep 2004 17:47:17 GMT]]></title><description><![CDATA[<p>Hi!<br />
Das Problem ist, dass der Thread erst startet, wenn die Funktion Start_Window_Observer() schon zu Ende ist, und dann wird die Variable dwThrdParam eben gelöscht. Entweder du machst ein Sleep(1); nach deinem CreateThread Aufruf, damit der Thread startet, bevor die Funktion Start_Window_Observer() zu Ende ist, oder du machst es so:</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt; 
#include &lt;conio.h&gt; 
#include &quot;stdio.h&quot;

DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
{ 
    char szMsg[80]; 

    wsprintf( szMsg, &quot;Parameter = %d.&quot;, *(DWORD*)lpParam ); 
    MessageBox( NULL, szMsg, &quot;ThreadFunc&quot;, MB_OK ); 

	delete (DWORD*)lpParam;

    return 0; 
} 

HANDLE Start_Window_Observer() 
{ 
    DWORD dwThreadId; 
    HANDLE hThread;
	DWORD* dwThrdParam = new DWORD;
	*dwThrdParam = 60;

    hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size   
        ThreadFunc,                  // thread function 
        dwThrdParam,                // argument to thread function 
        0,                           // use default creation flags 
        &amp;dwThreadId);                // returns the thread identifier 

	//Sleep(1);

    SetThreadPriority(hThread, THREAD_PRIORITY_ABOVE_NORMAL); 
    return hThread; 
} 

HANDLE Stop_Window_Observer(HANDLE hThread) 
{ 
    TerminateThread(hThread, 0); 
    CloseHandle(hThread); 
    hThread = NULL; 
    return hThread; 
} 

int main(int argc, char* argv[]) 
{ 
   HANDLE hThread; 
   printf(&quot;Window Observer gestartet\n\n&quot;); 
   hThread = Start_Window_Observer(); 
   while(getchar() != '\n'); 
   hThread = Stop_Window_Observer(hThread); 
   return 0; 
}
</code></pre>
<p>Hoffe, dir geholfen zu haben,<br />
Gruß,<br />
Kevin</p>
<p>P.S.: Sowas gehört sich ins WINAPI-Forum <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/601861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/601861</guid><dc:creator><![CDATA[Surkevin]]></dc:creator><pubDate>Tue, 07 Sep 2004 17:47:17 GMT</pubDate></item></channel></rss>