各位同行:
帮帮我:
怎么使用CreateThread创建线程??
能否解释一下各个参数吗?
先谢谢了!

解决方案 »

  1.   

    The following example demonstrates how to create a new thread that executes the locally defined function, ThreadFunc.#include <windows.h>
    #include <conio.h>DWORD WINAPI ThreadFunc( LPVOID lpParam ) 

        char szMsg[80];    wsprintf( szMsg, "Parameter = %d.", *(DWORD*)lpParam ); 
        MessageBox( NULL, szMsg, "ThreadFunc", MB_OK );    return 0; 

     
    VOID main( VOID ) 

        DWORD dwThreadId, dwThrdParam = 1; 
        HANDLE hThread; 
        char szMsg[80];    hThread = CreateThread( 
            NULL,                        // default security attributes 
            0,                           // use default stack size  
            ThreadFunc,                  // thread function 
            &dwThrdParam,                // argument to thread function 
            0,                           // use default creation flags 
            &dwThreadId);                // returns the thread identifier 
     
       // Check the return value for success. 
     
       if (hThread == NULL) 
       {
          wsprintf( szMsg, "CreateThread failed." ); 
          MessageBox( NULL, szMsg, "main", MB_OK );
       }
       else 
       {
          _getch();
          CloseHandle( hThread );
       }
    }
      

  2.   

    The CreateThread function creates a thread to execute within the virtual address space of the calling process.To create a thread that runs in the virtual address space of another process, use the CreateRemoteThread function.
    HANDLE CreateThread(
      LPSECURITY_ATTRIBUTES lpThreadAttributes,
      SIZE_T dwStackSize,
      LPTHREAD_START_ROUTINE lpStartAddress,
      LPVOID lpParameter,
      DWORD dwCreationFlags,
      LPDWORD lpThreadId
    );Parameters
    lpThreadAttributes 
    [in] 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. 
    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. The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator.dwStackSize 
    [in] Initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information, see Thread Stack Size. 
    lpStartAddress 
    [in] Pointer to the application-defined function to be executed by the thread and represents the starting address of the thread. For more information on the thread function, see ThreadProc. 
    lpParameter 
    [in] Pointer to a variable to be passed to the thread. 
    dwCreationFlags 
    [in] 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. Windows Server 2003 and Windows XP:  If the STACK_SIZE_PARAM_IS_A_RESERVATION flag is specified, the dwStackSize parameter specifies the initial reserve size of the stack. Otherwise, dwStackSize specifies the commit size.
    lpThreadId 
    [out] Pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.Windows Me/98/95:  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.Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
    Windows Me/98/95:  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.
    Res
    The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2028 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.The new thread handle is created with the THREAD_ALL_ACCESS access right. If a security descriptor is not provided, the handle can be used in any function that requires a thread object handle. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process cannot use the handle to gain access to the thread. If the thread impersonates a client, then calls CreateThread with a NULL security descriptor, the thread object created has a default security descriptor which allows access only to the impersonation token's TokenDefaultDacl owner or members. For more information, see Thread Security and Access Rights.The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.The ExitProcess, ExitThread, CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:Do not create a thread while impersonating another user. The call will succeed, however the newly created thread will have reduced access rights to itself when calling GetCurrentThread. The access rights granted are derived from the access rights the impersonated user has to the process. Some access rights including THREAD_SET_THREAD_TOKEN and THREAD_GET_CONTEXT may not be present, leading to unexpected failures.During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process. 
    Only one thread in a process can be in a DLL initialization or detach routine at a time. 
    ExitProcess does not return until no threads are in their DLL initialization or detach routines. A thread that uses functions from the static C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called. Note that this is not a problem with the C run-time in a DLL.Example Code 
    For an example, see Creating Threads.Requirements
    Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
    Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.
      

  3.   

    一楼是例子,虽然是cpp,但很简单
    二楼是函数的解释
    都是msdn里的
      

  4.   

    (1)创建一个线程。
      (2)创建一个能作为线程入口的函数。 
    Windows API调用CreateThread函数来创建一个线程。函数如下: HANDLE CreateThread(
      LPSECURITY_ATTRIBUTES:lpThreadAttributes;
                            //线程安全属性地址
      DWORD:dwStackSize;
                            //初始化线程堆栈尺寸
      LPTHREAD_START_ROUTINE:lpStartAddress;
                            //线程函数所指向的地址
      LPVOID:lpParameter;
                            //给线程函数传递的参数
      DWORD:dwCreationFlags;
                            //有关线程的标志
      LPDWORD:lpThreadId;
                           //系统分配给线程的ID 
    );第一个参数是安全属性,一般设为nil,使用缺省的安全属性。当我们想此线程有另外的子进程时,可改变它的属性。 第二个参数是线程堆栈尺寸,一般设为0,表示与此应用的堆栈尺寸相同,即主线程与创建的线程一样长度的堆栈。并且其长度会根据需要自动变长。 第三个参数,也是最重要的一个,是一个指向函数名的指针,但传递时很简单,只需在线程函数名前加上@就可以了。 第四个参数是你需要向线程函数传递的参数,一般是一个指向结构的指针。不需传递参数时,则这个参数设为nil。 第五个参数,传入与线程有关的一些标志,如果是CREATE_SUSPENDED,则创建一个挂起的线程,即这个线程本身已创建,它的堆栈也已创建。但这个线程不会被分配给CPU时间,只有当ResumeThread函数被调用后才能执行;当然,也可以调用SuspendThread函数再次挂起线程。要是标志为0,那么一旦建立线程,线程函数就被立即调用。一般传为0即可。 最后一个参数是系统分配给这个线程的唯一的ID标志。
      

  5.   

    var hThreadHandle: THandle;procedure CheckThread;
    begin
      while true do
        begin
          Sleep(1000);
          Inc(ActivateTime);
        end;
    end;hThreadHandle := CreateThread(nil, 0, @CheckThread, nil, 0, i);