// 比如:
Function(Par1, _tagPar2);
_tagWantUse = *_tagPar2;// 上述的2个步骤,如果不放在线程中就不会出现错误。
// 在线程中就出错,pThis = ClassDlg::static_cast<*ClassDlg>;(这个可能这里写的不对,但程序里肯定是对的),pThis也用了还是不行
大家帮我一下吧~呵呵……谢谢啦

解决方案 »

  1.   

    Function里是怎么实现的?
    可能跟tagPar2有关。
      

  2.   

    Par1, _tagPar2 是什么东西?MFC有关的函数都是与线程相关的,比如AfxGetMainWnd之类的。
      

  3.   

    ***********************************************************
    // pcmwf的定义为:StructSample * pStruct;全局的StructSample  StructUse;             // 线程中定义的
    pThis->Function(pBuf, pStruct);
    pThis->StructUse = * pStruct;        // 就是执行这步时在赋值的时候出错,
                                         // 具体的GetWaveData函数里面怎么实现应该不用贴出来吧?因为这2个步骤不在线程中时一切正常。只在线程中就出错
    *************************************************************
      

  4.   


    GetWaveData函数就是我说的Function,为了简化描述
      

  5.   

    检查一下pThis是否有意义?它是怎么传递的?
      

  6.   

    我都说晕了……呵呵,不好意思,上面的上个说错了
    应该是这样的:
    ============================================================
    // StructSample * StructHere ;全局的 StructSample  *pStruct;                 // 线程中定义的
    pThis->Function(pBuf, pStruct); 
    pThis->StructHere = * pStruct;          // 就是执行这步时在赋值的时候出错, 
                                            // 具体的Function函数里面怎么实现应该不用贴出来吧?因为这2个步骤不在线程中时一切正常。只在线程中就出错 
    =============================================================pThis就是在线程定义的:ClassDlg* pThis = static_cast<ClassDlg*>(lParam) // lParam就是线程的参数
      

  7.   

    是啊 怎么没有给pStruct赋值 
    调用Function后,pStruct的值不会变
      

  8.   

    问题找到了,是我创建线程时参数的问题,应该是第3个和最后一个参数写的方式不对,请教下大家CreateThread不同参数不用的意义了
    谢谢
      

  9.   

    pStruct 的内存空间有没有分配?
      

  10.   

    HANDLE WINAPI CreateThread(
      __in          LPSECURITY_ATTRIBUTES lpThreadAttributes,
      __in          SIZE_T dwStackSize,
      __in          LPTHREAD_START_ROUTINE lpStartAddress,  //线程函数地址
      __in          LPVOID lpParameter,
      __in          DWORD dwCreationFlags,
      __out         LPDWORD lpThreadId                     //线程的id,如果用不到id,可以设为NULL
    );
      

  11.   

    查MSDN
    ========================================================================================================================
    CreateThreadThe 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 token of the creator.Windows XP/2000/NT:  The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator. This behavior changed with Windows XP SP2 and Windows Server 2003.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. 
    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.Windows 2000/NT and Windows Me/98/95:  The STACK_SIZE_PARAM_IS_A_RESERVATION flag is not supported.
    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 that 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 in an executable that calls the C run-time library (CRT) should use the _beginthread and _endthread functions for thread management rather than CreateThread and ExitThread; this requires the use of the multi-threaded version of the CRT. It is safe to call CreateThread and ExitThread from a thread in a DLL that links to the static CRT as long as the thread does not call the DisableThreadLibraryCalls function.Example Code 
    For an example, see Creating Threads.Requirements
    Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95. 
    Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server. 
    Header Declared in Winbase.h; include Windows.h.
     
    Library Link to Kernel32.lib.
     
    DLL Requires Kernel32.dll.  
      

  12.   

    ……全是MSDN上的……
    我应该在创建线程的时候把第4个参数,写成this,这样就可以把类成员变量拿来用了,不会出现越界的问题了
    谢谢大家了