huaruta 发表于 2008-3-23 14:47

使用CreateThread创建线程并且传递参数问题

程序运行出现问题 请教高手
先谢
#include <windows.h>
#include <iostream.h>
DWORD WINAPI Func(LPVOID p);
typedef struct
{
        int firstArgu;
        long secArgu;
}myType,* pMyType;

void main()
{
        pMyType pMyTpeyArgu;
        pMyTpeyArgu->firstArgu=1;
         pMyTpeyArgu->secArgu=100000000;
        static HANDLE hHandle1;
        hHandle1=CreateThread(NULL,0,Func,pMyTypeArgu,0,NULL);
        Sleep(5000);
        cout<<"该线程已删除……"<<endl;
        ExitThread(0);
}
DWORD WINAPI Func(LPVOID p)
{   
       
    int   intValue=((pMyType)LPVOID)->firstArgu;
        long longValue=((pMyType)LPVOID)->secArgu;
        cout <<"Thread is running,线程正在运行,5秒钟后该线程将自动删除"<<endl;
        cout <<"现在输出该线程函数传递的两个参数:"<<endl;
    cout <<"firstArgu="<<intValue<<";"<<"secArgu="<<longValue<<"."<<endl;
        cout<<"请等待……"<<endl<<endl;
        return(0);
}

yaoxin 发表于 2008-5-17 20:22

#include "stdafx.h"
#include <windows.h>
#include <iostream>

using namespace std;

DWORD WINAPI Func(LPVOID p);
typedef struct
{
    int firstArgu;
    long secArgu;

}MyType,* pMyType;

void main()
{
    MyType MyTypeArgu={0};
    MyTypeArgu.firstArgu=1;
    MyTypeArgu.secArgu=100000000;
    CreateThread(NULL,0,Func,&MyTypeArgu,0,NULL);
    Sleep(5000);
    cout<<"该线程已删除……"<<endl;
    ExitThread(0);
}
DWORD WINAPI Func(LPVOID p)
{   
    MyType param={0};
    MoveMemory(&param,p,sizeof(param));
    int   intValue=param.firstArgu;
    long longValue=param.secArgu;
    cout <<"Thread is running,线程正在运行,5秒钟后该线程将自动删除"<<endl;
    cout <<"现在输出该线程函数传递的两个参数:"<<endl;
    cout <<"firstArgu="<<intValue<<endl<<"secArgu="<<longValue<<"."<<endl;
    cout<<"请等待……"<<endl<<endl;
    return 0;
}
//-------------------------------------------------------------------------
上面程序在Visual Studio 2005中编译通过!
建议LZ先把E文好好学一下~~~

页: [1]

编程论坛