Results 1 to 6 of 6

Thread: Correct Way To Use CreateThread

  1. #1

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    Question Correct Way To Use CreateThread

    Whats the correct way to use CreateThread ? can somebody give me a tutorial, or a sample app in c++ ?

  2. #2
    Member
    Join Date
    Feb 2001
    Posts
    57

    Not a tutorial, but this will start you off...

    CreateThread
    The CreateThread function creates a thread to execute within the address space of the calling process.

    HANDLE CreateThread(
    LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes
    DWORD dwStackSize, // initial thread stack size
    LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
    LPVOID lpParameter, // argument for new thread
    DWORD dwCreationFlags, // creation flags
    LPDWORD lpThreadId // pointer to receive thread ID
    );

    Parameters
    lpThreadAttributes
    Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
    Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.

    dwStackSize
    Specifies the initial commit size of the stack, in bytes. The system rounds this value to the nearest page. If this value is zero, or is smaller than the default commit size, the default is to use the same size as the calling thread. For more information, see Thread Stack Size.

    lpStartAddress
    Pointer to the application-defined function of type LPTHREAD_START_ROUTINE to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc.

    lpParameter
    Specifies a single 32-bit parameter value passed to the thread.

    dwCreationFlags
    Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.

    lpThreadId
    Pointer to a 32-bit variable that receives the thread identifier.
    Windows NT: If this parameter is NULL, the thread identifier is not returned.

    Windows 95 and Windows 98: This parameter may not be NULL.

    Return Values
    If the function succeeds, the return value is a handle to the new thread.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

    Windows 95 and Windows 98: CreateThread succeeds only when it is called in the context of a 32-bit program. A 32-bit DLL cannot create an additional thread when that DLL is being called by a 16-bit program.

  3. #3

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411

    uhh

    yo i have the help files too. hehe i've gotten the function working, but just not correctly. i'm doing something wrong and i don't know what, i get wierd results when i use the CreateThread function, like for instance, my movie doesn't show, its playing, but its not showing it shows fine without the createthread, but for somereason when i use the function, it doesn't show oh well

  4. #4
    Member
    Join Date
    Feb 2001
    Posts
    57

    An example...

    Code:
    #include <genstub.cpp>
    
    // Child thread procedure just displays a message box with information.
    DWORD WINAPI ChildThreadProc(HWND hWnd)
     {
        static char szBuffer[256];
        wsprintf(szBuffer, "Process Handle = %x, ID = %x, Thread: Handle = %x, ID = %x",
                 GetCurrentProcess(), GetCurrentProcessId(),
                 GetCurrentThread(), GetCurrentThreadId());
        MessageBox(hWnd, szBuffer, "Process/Thread Report", MB_OK);
        ExitThread(TRUE);
     }
    
    // Main Window Procedure
    LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
     {
       switch (uMsg)
        {
          case WM_COMMAND:       // Process menu items.
                  switch (LOWORD(wParam))
                   {
                     case IDM_TEST:
                               {
                                 DWORD dwChildId;
                                 CreateThread(NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId);
                               }
                              break;
                     case IDM_EXIT:
                              DestroyWindow(hWnd);
                              break;
                    }
                   break;
          case WM_DESTROY:
                  PostQuitMessage(0);
                  break;
          default:
                  return DefWindowProc(hWnd, uMsg, wParam, lParam);
       }
       return (NULL);
    }

  5. #5

    Thread Starter
    Hyperactive Member AAG's Avatar
    Join Date
    Aug 2000
    Location
    United States
    Posts
    411
    THANX FOR THE CODE, BUT I ALREADY NEW ALL THAT. Your code had quite a few errors also. first of all, in your createthread procedure, you had

    CreateThread(NULL, 0, ChildThreadProc, hWnd, 0, &dwChildId);

    your supposed to have

    CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) ChildThreadProc, hWnd, 0, &dwChildId);


    you included a .cpp file which doesn't exist. the thread function displays a message box with info, but theres no way to get click OK on the message box i've fixed the errors, and i'm back right where i started, i'll figure it out, thanx anyways.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Attached Files Attached Files
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width