这里只把两个关键函数写出来,其他部分应该没影响
运行的时候,GetFile()函数确实创建了cpContext->RunningTaskInfo.iThread个文件,
表明确实创建了cpContext->RunningTaskInfo.iThread个线程
可是下载的时候却是先是第一个文件下完,然后第二个.....
这样的结果比单线程慢很多
声明:下载过程没问题,文件能用//获得文件大小,并创建N个子线程进行下载
void DownLoad(pRunningTask cpContext)
{
HINTERNET    hRequest=NULL;
         DWORD        dwSize=0;
LPVOID       lpOutBuffer;
char         szError[80];
int          iSize=0;
int          i;
pChildThread pctInfo;
     retry: if(!HttpQueryInfo(cpContext->RunningTaskInfo.hResource,HTTP_QUERY_CONTENT_LENGTH,(LPVOID)lpOutBuffer,&dwSize,NULL))
{
//check if the header was not found
if (GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND)
{
wsprintf(szError,"Error %d encountered", GetLastError());

//显示错误信息
                           DisplayErr(szError);
return ;
}
else
{
//存储Header信息的内存不够
if (GetLastError()==ERROR_INSUFFICIENT_BUFFER)
{
//添加内存空间
lpOutBuffer = new char[dwSize+1];
goto retry;
}
else
{
//display other errors in header edit box
wsprintf(szError,"Error %d encountered", GetLastError());
    //显示错误信息
    
    DisplayErr(szError);
return;
}
}
}
cpContext->RunningTaskInfo.FileSize=(DWORD)atol((PSTR)lpOutBuffer);
    wsprintf(szError,"分析文件大小成功:%ldb",cpContext->RunningTaskInfo.FileSize);
DisplayErr(szError);
//根据奴隶数,创建子线程进行下载
for(i=0;i<cpContext->RunningTaskInfo.iThread;i++)
{
pctInfo=(pChildThread)malloc(sizeof(ChildThread));

pctInfo->ParentThread=cpContext;//关联父进程信息
//最后一个线程,把余数部分加起来下完
if(i==cpContext->RunningTaskInfo.iThread-1)
{
pctInfo->Begin = i*(cpContext->RunningTaskInfo.FileSize)/(cpContext->RunningTaskInfo.iThread);
pctInfo->End   = (i+1)*(cpContext->RunningTaskInfo.FileSize)/(cpContext->RunningTaskInfo.iThread)
            +(cpContext->RunningTaskInfo.FileSize)%(cpContext->RunningTaskInfo.iThread);
}
else
{
pctInfo->Begin = i*(cpContext->RunningTaskInfo.FileSize)/(cpContext->RunningTaskInfo.iThread);
pctInfo->End   = (i+1)*(cpContext->RunningTaskInfo.FileSize)/(cpContext->RunningTaskInfo.iThread);
}
pctInfo->i=i;
//GetFile(pctInfo);
CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)GetFile,LPVOID(pctInfo), 0,
NULL);
}
        
}//下面的函数实现真正的下载
void GetFile(pChildThread pctInfo)
{
HANDLE    hFile;
char      Buffer[2049];
DWORD     dwRequest=1024,
 dwRead,
 dwWrite=0;
BOOL      ReadReturn;
char  szError[80];
         char      temp[8];
         HINTERNET ChildResource;
int       i,j,k;
pChildThread pctInfoThis;//此线程信息

//防止两个线程命名冲突
//WaitForSingleObject(ChildEvent,INFINITE);
pctInfoThis=pctInfo;
wsprintf(temp,"%d",pctInfoThis->i);
strcat(temp,pctInfoThis->ParentThread->RunningTaskInfo.FileNewName);
hFile=CreateFile(temp,GENERIC_WRITE,0,0,CREATE_NEW,0,0);
//SetEvent(ChildEvent); k=GetLastError(); //设置读文件位置
//如果用全局变量pctInfo->ParentThread->hResource的话,每个线程都对其进行修改
//后果无法预料
//所以这里新建一个HINTERNET
ChildResource=InternetOpenUrl(hOpen,pctInfoThis->ParentThread->RunningTaskInfo.URL,NULL,
                             0,INTERNET_FLAG_RELOAD,0);
InternetSetFilePointer(ChildResource,pctInfoThis->Begin,NULL,FILE_BEGIN,0);

//确定读数据的次数
i=(pctInfoThis->End-pctInfoThis->Begin)/dwRequest;
for(j=0;j<i;)
{   
if(j==i-1)
ReadReturn=InternetReadFile(ChildResource,Buffer,dwRequest+(pctInfoThis->End-pctInfoThis->Begin)%dwRequest,&dwRead);
else
ReadReturn=InternetReadFile(ChildResource,Buffer,dwRequest,&dwRead);
//if(GetLastError()==ERROR_IO_PENDING)break;
        k=GetLastError();
if(!ReadReturn)break;
if(dwRead==0)break;
k=GetLastError(); Buffer[dwRead]='\0';
WriteFile(hFile,Buffer,dwRead,&dwWrite,0);
k=GetLastError();
j++;
}
}