_beginthreadex与WaitForSingleObject

#include <stdio.h> 
#include <string.h>
#include <conio.h>
#include <IOSTREAM.H> 
#include <Windows.h>
#include <process.h>

bool CheckLastRate(char *lpContent);
unsigned int _stdcall Check_Thread(LPTSTR lpContent);

int	ProcessRate = 0;

int main()
{
    FILE *stream;
	int ch;
	int i = 0;
	char content[1000];
	HANDLE	hHandle = NULL;

	memset(content,0,1000);
	stream = fopen("d:\\WIMrepairRead.txt","r");
	

	fseek(stream, 0, SEEK_SET);

    do
    {
 
        ch = fgetc(stream);

		content[i] = ch ;
		i++;
    } 
    while (ch != EOF);
	cout<<content<<endl;
    fclose(stream);



	hHandle = (HANDLE)_beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Check_Thread,content,0,NULL);
	


		WaitForSingleObject(hHandle,INFINITE );
		printf("\nsucceed");


    return 0;
}

bool CheckLastRate(char *lpContent)
{
	/*cout<<"lpContent:"<<lpContent<<endl;*/
	int iIndex = 0;
	int iLastPercentPosition = 0;
	int iPercentNum = 0;
	
	
	for (;iIndex<strlen(lpContent);iIndex++)
	{
		
		if (lpContent[iIndex]=='%')
		{
			iPercentNum++;
			iLastPercentPosition = iIndex ;
		}
	}

	if (iPercentNum!=0)				//有百分号的情况
	{
		//是两位数或三位数的情况
	
		if (lpContent[iLastPercentPosition-2]>48 && lpContent[iLastPercentPosition-2]<=57)
		{
			ProcessRate = (lpContent[iLastPercentPosition-2]-'0')*10+(lpContent[iLastPercentPosition-1]-'0');
			
			if (ProcessRate == 0)  //三位数的情况
			{
				ProcessRate = 100;
			}
		}

		//是一位数的情况
		else 
		{
			ProcessRate = lpContent[iLastPercentPosition-1]-'0';
		}
		
		cout<<ProcessRate<<endl;
		return true;
	}
	else
	{
		return false;	
	}
	
}
unsigned int _stdcall Check_Thread(LPTSTR lpContent)
{
	if(CheckLastRate(lpContent))
	{
		cout<<endl<<"发现"<<endl;
	}
	else
	{
		cout<<endl<<"no发现"<<endl;
	}
	Sleep(10000);
	return 0;
}
对比下一段代码
#include <stdio.h> 
#include <string.h>
#include <conio.h>
#include <IOSTREAM.H> 
#include <Windows.h>
#include <process.h>

bool CheckLastRate(char *lpContent);
unsigned int _stdcall Check_Thread(LPTSTR lpContent);

int	ProcessRate = 0;

int main()
{
    FILE *stream;
	int ch;
	int i = 0;
	char content[1000];
	DWORD	dwRet = (DWORD)-1;
	HANDLE  hHandle = NULL ;

	memset(content,0,1000);
	stream = fopen("d:\\WIMrepairRead.txt","r");
	

	fseek(stream, 0, SEEK_SET);

    do
    {
 
        ch = fgetc(stream);

		content[i] = ch ;
		i++;
    } 
    while (ch != EOF);
	cout<<content<<endl;
    fclose(stream);



	dwRet = _beginthreadex(NULL,0,(unsigned int (__stdcall *)(void *))Check_Thread,content,0,NULL);
	
	if (dwRet==(DWORD)-1)
	{
		cout<<"failed"<<endl;
	}
	else
	{
		cout<<"succeed"<<endl;
	}

		//WaitForSingleObject(hHandle,INFINITE );
		printf("\nsucceed");


    return 0;
}

bool CheckLastRate(char *lpContent)
{
	/*cout<<"lpContent:"<<lpContent<<endl;*/
	int iIndex = 0;
	int iLastPercentPosition = 0;
	int iPercentNum = 0;
	
	
	for (;iIndex<strlen(lpContent);iIndex++)
	{
		
		if (lpContent[iIndex]=='%')
		{
			iPercentNum++;
			iLastPercentPosition = iIndex ;
		}
	}

	if (iPercentNum!=0)				//有百分号的情况
	{
		//是两位数或三位数的情况
	
		if (lpContent[iLastPercentPosition-2]>48 && lpContent[iLastPercentPosition-2]<=57)
		{
			ProcessRate = (lpContent[iLastPercentPosition-2]-'0')*10+(lpContent[iLastPercentPosition-1]-'0');
			
			if (ProcessRate == 0)  //三位数的情况
			{
				ProcessRate = 100;
			}
		}

		//是一位数的情况
		else 
		{
			ProcessRate = lpContent[iLastPercentPosition-1]-'0';
		}
		
		cout<<ProcessRate<<endl;
		return true;
	}
	else
	{
		return false;	
	}
	
}
unsigned int _stdcall Check_Thread(LPTSTR lpContent)
{
	if(CheckLastRate(lpContent))
	{
		cout<<endl<<"发现"<<endl;
	}
	else
	{
		cout<<endl<<"no发现"<<endl;
	}
	Sleep(10000);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值