+ Reply to Thread
Results 1 to 2 of 2

Thread: How to get threads of current process?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    46

    How to get threads of current process?

    I want to get list of threads for my MFC application.

    Can I get this list?

    Is there any MFC class available for this?

    Thanks in advance.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    38
    You can use CreateToolhelp32Snapshot, Thread32First and Thread32Next for this purpose.

    Example:

    Code:
    void GetProcessThreads()
    {
    	DWORD dwCurrentPtocessID = GetCurrentProcessId() ;
    	HANDLE hThreadSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0) ;
    	if(INVALID_HANDLE_VALUE != hThreadSnapshot)
    	{
    		THREADENTRY32 objThreadEntry32 ;
    		objThreadEntry32.dwSize = sizeof(objThreadEntry32) ;
    
    		if (Thread32First(hThreadSnapshot, &objThreadEntry32))
    		{
    			do
    			{
    				if (dwCurrentPtocessID == objThreadEntry32.th32OwnerProcessID)
    				{
    					char szThread[1024] ;
    					sprintf(szThread, "Thread ID : %d\n", objThreadEntry32.th32ThreadID) ;
    					OutputDebugString(szThread) ;
    				}
    			}
    			while(Thread32Next(hThreadSnapshot, &objThreadEntry32)) ;
    		}
    
    		CloseHandle(hThreadSnapshot) ;
    	}
    }
    Hope this helps.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts