Thread aufruf



  • Hallo Leute,
    bin gerade dabei mich in WinAPI einzuarbeiten.

    Ich habe bei folgendem Code das Problem, dass ich nicht mehr in das Main zurückkomme. Warum? Eigentlich sollte ich während die Threads abgearbeitet werden wieder kurzzeitig ins Main kommen?

    Ich hoffe ihr könnt mir einen Tipp geben.

    Viele Grüße

    Reiner

    #include <windows.h>
    #include <stdio.h>
    #include <process.h>
    #include <iostream>
    using namespace std;
    
    DWORD WINAPI myfunc (LPVOID);
    
    DWORD b;
    
    DWORD ThreadID;
    HANDLE HandleThread;
    BOOL Exit;
    DWORD status;
    BOOL code;
    
    DWORD Test;
    DWORD arg1=7;
    DWORD arg2=4;
    
    void main ()
    {
    
    	b=30;
    	HandleThread = CreateThread(NULL,0,myfunc,(LPVOID)arg1,0,&ThreadID); 
    
    	if(HandleThread == NULL)
    	{
    		cout<<"Fehlerhafte Thread-Erzeugung"<<endl<<endl;
    	}
    
    	cout<<"Thread-Handle: "<<HandleThread<<"\tThreadID: "<<ThreadID<<endl<<endl;
    
    	myfunc((LPVOID)arg2);
    
    	Sleep(700);
    
    	DWORD exitCode;
    	Exit = GetExitCodeThread(HandleThread,&exitCode);
    	if(Exit == false)
    	{
    		cout<<"Exit Code konnte nicht abgefragt werden";
    	}
    	else
    	{
    
    			switch(exitCode)
    				{
    					case STILL_ACTIVE:	cout<<"STILL_ACTIVE"<<endl;
    										break;
    
    					case 23:	cout<<"Thread beendet"<<endl;
    								break;
    
    					default:	cout<<"unbekannt"<<endl;
    								break;
    				}
    	}
    
    	status = WaitForSingleObject (HandleThread, INFINITE);
    
    	if(status==WAIT_FAILED)
    	{
    		cout<<endl<<"Warten fehlgeschlagen"<<endl;
    	}
    	else
    	{
    
    			code = GetExitCodeThread(HandleThread,&exitCode);
    			if(code == false)
    				cout<<"Exit Code Abfrage Fehlerhaft"<<endl;
    
    			else
    			{
    				switch(exitCode)
    				{
    					case STILL_ACTIVE:	cout<<"STILL_ACTIVE"<<endl;
    										break;
    
    					case 23:	cout<<"Thread beendet"<<endl;
    								break;
    
    					default:	cout<<"unbekannt"<<endl;
    								break;
    				}
    
    				cout<<"Wert von b: "<<b<<endl<<endl;
    			}
    	}
    
    }
    
    DWORD WINAPI myfunc (void* n)
    {
    	DWORD arg;
    	arg = (DWORD)n;
    
    	DWORD funkTid = GetCurrentThreadId();
    
    	for(int i=0; i<=(int)arg; i++)
    	{
    		Sleep(1000);
    		cout<<endl<<"Thread-ID: "<<funkTid<<" i= "<<i<<" arg= "<<arg<<endl; 
    		b=b+10;
    	}
    	ExitThread(23);
    	return 0;
    }
    


  • Du hast die Hilfe aber schon bis zu Ende gelesen?

    A thread in an executable that calls the C run-time library (CRT) should use the _beginthreadex and _endthreadex functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.



  • Thx vielen Dank für den Hinweis


Anmelden zum Antworten