Click here to Skip to main content
15,919,749 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: delete memory Pin
Nishad S10-Aug-07 2:17
Nishad S10-Aug-07 2:17 
AnswerRe: delete memory Pin
KarstenK10-Aug-07 2:17
mveKarstenK10-Aug-07 2:17 
GeneralRe: delete memory Pin
David Crow10-Aug-07 3:19
David Crow10-Aug-07 3:19 
GeneralRe: delete memory Pin
KarstenK10-Aug-07 3:27
mveKarstenK10-Aug-07 3:27 
GeneralRe: delete memory Pin
David Crow10-Aug-07 3:29
David Crow10-Aug-07 3:29 
GeneralRe: delete memory Pin
KarstenK10-Aug-07 3:40
mveKarstenK10-Aug-07 3:40 
GeneralRe: delete memory Pin
David Crow10-Aug-07 3:50
David Crow10-Aug-07 3:50 
QuestionQuery regarding terminating a thread. Pin
Rajesh_Parameswaran10-Aug-07 1:23
Rajesh_Parameswaran10-Aug-07 1:23 
Hi,
I'm having an application which connects to a database. I'm calling the SQLConnect() function in a seperate thread, so that if the connection is not there, it should time-out in 10 sec. and if the thread times out, I'm calling terminatethread. Is this the right way of doing? or is there any other way to terminate the thread? NB: SQLConnect() is a blocking call.

Please find the code snippet.

void DBConnectThread(void* param);
HANDLE hThread;
HANDLE hMutex;

int main()
{
unsigned long ThreadID=NULL;
unsigned long ExitCode=0;
DWORD retCode;

hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DBConnectThread, 0, 0, &ThreadID);

if (WAIT_TIMEOUT == WaitForSingleObject(hThread, 10000))
{
GetExitCodeThread(hThread, &ExitCode);
TerminateThread(hThread, ExitCode);
}

if (hThread != NULL)
CloseHandle(hThread);

return 0;
}

void DBConnectThread(void *param)
{
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
SQLRETURN retcode;
SQLINTEGER rgbValue = 0;

DWORD startTickCount = 0;

unsigned char username[255] = "blue";
unsigned char password[255] = "_abc97";

SQLCHAR * OutConnStr = (SQLCHAR * )malloc(255);
SQLSMALLINT * OutConnStrLen = (SQLSMALLINT *)malloc(255);

// Allocate environment handle
retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);

// Set the ODBC version environment attribute
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{

retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, SQL_IS_INTEGER);

cout << "setting odbc version" << endl;

// Allocate connection handle
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);

cout << "Allocating SQL Handle"<< endl;

// Set login timeout to 5 seconds
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{

cout << "Attempt to Connect" << endl;

startTickCount = GetTickCount();

// Connect to data source
retcode = SQLConnect(hdbc, (SQLCHAR*) "OQTUAM blue", SQL_NTS, username, SQL_NTS, password, SQL_NTS);

cout << "Connection Done in "<< (GetTickCount() - startTickCount) << " Secs" << endl;

// Allocate statement handle
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{

cout << "Connection Successful"<< endl;
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);

// Process data
if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO)
{
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);

}

SQLDisconnect(hdbc);
}
else
{
cout << "Failed to Connect" << endl;
}

SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
}
}
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
}

thanks in advance,

Rajesh
AnswerRe: Query regarding terminating a thread. Pin
jhwurmbach10-Aug-07 1:42
jhwurmbach10-Aug-07 1:42 
AnswerRe: Query regarding terminating a thread. Pin
KarstenK10-Aug-07 3:15
mveKarstenK10-Aug-07 3:15 
GeneralRe: Query regarding terminating a thread. Pin
Rajesh_Parameswaran10-Aug-07 5:56
Rajesh_Parameswaran10-Aug-07 5:56 
GeneralRe: Query regarding terminating a thread. Pin
KarstenK12-Aug-07 21:21
mveKarstenK12-Aug-07 21:21 
Questionhow to output text file in C++/MFC Pin
tunminshein10-Aug-07 0:56
tunminshein10-Aug-07 0:56 
AnswerRe: how to output text file in C++/MFC Pin
baerten10-Aug-07 1:08
baerten10-Aug-07 1:08 
AnswerRe: how to output text file in C++/MFC Pin
Hamid_RT10-Aug-07 1:11
Hamid_RT10-Aug-07 1:11 
AnswerRe: how to output text file in C++/MFC Pin
KarstenK10-Aug-07 1:14
mveKarstenK10-Aug-07 1:14 
AnswerRe: how to output text file in C++/MFC Pin
Anurag Gandhi10-Aug-07 1:23
professionalAnurag Gandhi10-Aug-07 1:23 
AnswerRe: how to output text file in C++/MFC Pin
jhwurmbach10-Aug-07 1:34
jhwurmbach10-Aug-07 1:34 
Questionregarding statusbar display Pin
mirraa10-Aug-07 0:21
mirraa10-Aug-07 0:21 
AnswerRe: regarding statusbar display Pin
Hamid_RT10-Aug-07 0:38
Hamid_RT10-Aug-07 0:38 
AnswerRe: regarding statusbar display Pin
KarstenK10-Aug-07 2:21
mveKarstenK10-Aug-07 2:21 
QuestionHow to resolve this compile time error? Pin
Mushtaque Nizamani10-Aug-07 0:01
Mushtaque Nizamani10-Aug-07 0:01 
AnswerRe: How to resolve this compile time error? Pin
Cedric Moonen10-Aug-07 0:09
Cedric Moonen10-Aug-07 0:09 
GeneralRe: How to resolve this compile time error? Pin
Mushtaque Nizamani10-Aug-07 0:15
Mushtaque Nizamani10-Aug-07 0:15 
AnswerRe: How to resolve this compile time error? Pin
prasad_som10-Aug-07 0:10
prasad_som10-Aug-07 0:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.