Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Query regarding terminating a thread.

Query regarding terminating a thread.

Scheduled Pinned Locked Moved C / C++ / MFC
databasequestionannouncementworkspace
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rajesh_Parameswaran
    wrote on last edited by
    #1

    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 = SQLAlloc

    J K 2 Replies Last reply
    0
    • R Rajesh_Parameswaran

      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 = SQLAlloc

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      Rajesh_Parameswaran wrote:

      I'm calling terminatethread. Is this the right way of doing?

      No. TeminateThread() is almost always the wrong solution. Use a variable whose pointer you hand to the thread, and have the thread terminate itself when the outside pulls this variable to false. Also, read the following stuff (made by Christian Grauss who is a codeproject regular): The Processes Articles[^]


      Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
      George Orwell, "Keep the Aspidistra Flying", Opening words

      1 Reply Last reply
      0
      • R Rajesh_Parameswaran

        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 = SQLAlloc

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        You better let the SQL-Stuff run completly. Normally you can somewhere else set a proper timeout for the DB. Or you have to wait the time. Otherwise you can damage your Database and THAT is an overkill accident X| Check the material for the database for the details

        Greetings from Germany

        R 1 Reply Last reply
        0
        • K KarstenK

          You better let the SQL-Stuff run completly. Normally you can somewhere else set a proper timeout for the DB. Or you have to wait the time. Otherwise you can damage your Database and THAT is an overkill accident X| Check the material for the database for the details

          Greetings from Germany

          R Offline
          R Offline
          Rajesh_Parameswaran
          wrote on last edited by
          #4

          Hi jhwurmbach/KarstenK, Thanks for your response. jhwurmbach, but the problem is that the call to SQLConnect() in the thread is a blocking one. So until i get a connection or it will timeout, it will be holding the call. It is getting timeout only after 40 seconds, that is a huge delay. I'm unable to set the ODBC Connection time out also. Even I set the ODBC time-out, it is taking it as WAIT_INFINITE. Any other alternative methods?? thanks in advance, Rajesh

          K 1 Reply Last reply
          0
          • R Rajesh_Parameswaran

            Hi jhwurmbach/KarstenK, Thanks for your response. jhwurmbach, but the problem is that the call to SQLConnect() in the thread is a blocking one. So until i get a connection or it will timeout, it will be holding the call. It is getting timeout only after 40 seconds, that is a huge delay. I'm unable to set the ODBC Connection time out also. Even I set the ODBC time-out, it is taking it as WAIT_INFINITE. Any other alternative methods?? thanks in advance, Rajesh

            K Offline
            K Offline
            KarstenK
            wrote on last edited by
            #5

            I strongly advise you to respect the Database (DB) and its rules. If you get in trouble with the Integrety of the DByou are in the biggest trouble. Dont ferget that the DB needs some reaction time and some work to do so it needs the time. If it is a professional DB on a network 40 seconds arent a big delay. Make a progress bar or a wait dialog to show the process. Whether you findnt this approbiate contact the adminstrators of the DB to discuss the issue. And inform me please what they said.

            Greetings from Germany

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups