kathyayini
Programmer
I am using thread to run Stored procedure which is in SQL Server.
for(IThreadCount=0;IThreadCount<3;IThreadCount++)
{
CreateThread(NULL,0,run_thread,(LPVOID)0,0,&dwThreadId[IThreadCount]);
SetThreadPriority(&dwThreadId[IThreadCount],THREAD_PRIORITY_HIGHEST);
Sleep(1000);
}
DWORD WINAPI run_thread(LPVOID Pntr)
{
try
{
Sleep(0);
TESTHR(pCommand.CreateInstance(__uuidof(Command)));
pCommand->ActiveConnection = pConnection;
pCommand->CommandText = (_bstr_t)ProcedureName[IThreadCount];
pCommand->CommandType = adCmdStoredProc;
pCommand->CommandTimeout = 15;
TESTHR(pParameter.CreateInstance(__uuidof(Parameter)));
pParameter->Type = adVarChar;
pParameter->Size = 3;
pParameter->Direction = adParamInput;
pParameter->Value = "RG";
pCommand->Parameters->Append(pParameter);
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);
success_flag=success_flag+1;
return TRUE; // thread completed successfully
}
catch(_com_error &f)
{
fail_flag=fail_flag+1;
return FALSE;
}
}
while((success_flag<IThreads) && (fail_flag==0))
{
//This loop is just to wait till all threads gets over.
}
for loop is to run 3 seperate threads which calls same function run_thread. while loop is to make the program wait till all the thread gets over. Problem is if i remove Sleep(1000) statement or make 1000 to 100, the program will not execute all threads properly. It will not set success_flag or fail_flag properly. The program suppose to increment success_flag,if thread executes Stored Procedure successfully else it will increment fail_flag flag. But it is not setting properly if i write Sleep(100) instead of Sleep(1000).
waiting for the reply
for(IThreadCount=0;IThreadCount<3;IThreadCount++)
{
CreateThread(NULL,0,run_thread,(LPVOID)0,0,&dwThreadId[IThreadCount]);
SetThreadPriority(&dwThreadId[IThreadCount],THREAD_PRIORITY_HIGHEST);
Sleep(1000);
}
DWORD WINAPI run_thread(LPVOID Pntr)
{
try
{
Sleep(0);
TESTHR(pCommand.CreateInstance(__uuidof(Command)));
pCommand->ActiveConnection = pConnection;
pCommand->CommandText = (_bstr_t)ProcedureName[IThreadCount];
pCommand->CommandType = adCmdStoredProc;
pCommand->CommandTimeout = 15;
TESTHR(pParameter.CreateInstance(__uuidof(Parameter)));
pParameter->Type = adVarChar;
pParameter->Size = 3;
pParameter->Direction = adParamInput;
pParameter->Value = "RG";
pCommand->Parameters->Append(pParameter);
pRecordset = pCommand->Execute(NULL,NULL,adCmdStoredProc);
success_flag=success_flag+1;
return TRUE; // thread completed successfully
}
catch(_com_error &f)
{
fail_flag=fail_flag+1;
return FALSE;
}
}
while((success_flag<IThreads) && (fail_flag==0))
{
//This loop is just to wait till all threads gets over.
}
for loop is to run 3 seperate threads which calls same function run_thread. while loop is to make the program wait till all the thread gets over. Problem is if i remove Sleep(1000) statement or make 1000 to 100, the program will not execute all threads properly. It will not set success_flag or fail_flag properly. The program suppose to increment success_flag,if thread executes Stored Procedure successfully else it will increment fail_flag flag. But it is not setting properly if i write Sleep(100) instead of Sleep(1000).
waiting for the reply