kill COM calls in thread
-
viperlogic wrote:
what is the best way of killing a thread from outside the thread, i am using TerminateThread() but i dont think this is the best way??
Set a boolean varible depending on it state terminate the thread
what command do i use to kill the thread thou from outside the thread?
-
what command do i use to kill the thread thou from outside the thread?
-
i have a thread that calls the following CoInitializeEx() CoInitializeSecurity() CoCreateInstance() ConnectServer() if the thread isnt successful in 3secs (using WaitForSingleObject(pThread->m_hThread, 3000)) it is closed. what is the best way of killing a thread from outside the thread, i am using TerminateThread() but i dont think this is the best way?? it appears thou that the calls in the thread are not being killed off because if i go to start the thread again CoInitializeSecurity() throws up an error saying that it cant initialize security. so iam assuming it is saying this because the thread wasnt killed off right plz help
viperlogic wrote:
what is the best way of killing a thread from outside the thread...
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
viperlogic wrote:
what command do i use to kill the thread thou from outside the thread?
same, change the value of boolean variable from outside, which thread is checking from inside...
sorry, i dont understand, example please thanks
-
viperlogic wrote:
what is the best way of killing a thread from outside the thread...
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
so i use the following to delete a thread from outside?? Does "delete pThread" call ExitThread?? switch(WaitForSingleObject(pThread->m_hThread, 3000)) { case WAIT_TIMEOUT: { delete pThread; break; } case WAIT_OBJECT_0: { // thread has terminated on its own break; } }
-
so i use the following to delete a thread from outside?? Does "delete pThread" call ExitThread?? switch(WaitForSingleObject(pThread->m_hThread, 3000)) { case WAIT_TIMEOUT: { delete pThread; break; } case WAIT_OBJECT_0: { // thread has terminated on its own break; } }
viperlogic wrote:
Does "delete pThread" call ExitThread??
No. At the point in which you are deleting
pThread
, the thread has already terminated. You are just cleaning up theCWinThread
object.ExitThread()
is called from within the secondary thread, unlikeTerminateThread()
which is called from outside the secondary thread.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
viperlogic wrote:
Does "delete pThread" call ExitThread??
No. At the point in which you are deleting
pThread
, the thread has already terminated. You are just cleaning up theCWinThread
object.ExitThread()
is called from within the secondary thread, unlikeTerminateThread()
which is called from outside the secondary thread.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
i think i have that working now. A kind of related problem i have is with the code below. This is a part of the code the thread executes, if sucessful all is ok. this thread has a timeout of 3secs. A problem is that the ConnectServer call below takes 2minutes to complete if it cant connect. when this happens the below messagebox pops ups and then the program crashes. is it crashing because the code wants to "return" but there is nothing to return to as the thread has been already killed?? How can i modify it so that it wont crash. A message box doesnt have to be displayed either. many thanks
hres = pLoc_test->ConnectServer( _bstr_t(L"\\\\"+strIP1+"\\root\\cimv2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current WBEM_FLAG_CONNECT_USE_MAX_WAIT, // Security flags 0, // Authority 0, // Context object &pSvc_test // pointer to IWbemServices proxy ); if (FAILED(hres)) { char error_msg[64]; sprintf(error_msg, "wmi_test: Could not connect. Error code = 0x%x", hres); MessageBox(error_msg , "Error" , MB_ICONERROR | MB_OK); pLoc_test->Release(); return; }
-
i think i have that working now. A kind of related problem i have is with the code below. This is a part of the code the thread executes, if sucessful all is ok. this thread has a timeout of 3secs. A problem is that the ConnectServer call below takes 2minutes to complete if it cant connect. when this happens the below messagebox pops ups and then the program crashes. is it crashing because the code wants to "return" but there is nothing to return to as the thread has been already killed?? How can i modify it so that it wont crash. A message box doesnt have to be displayed either. many thanks
hres = pLoc_test->ConnectServer( _bstr_t(L"\\\\"+strIP1+"\\root\\cimv2"), // Object path of WMI namespace NULL, // User name. NULL = current user NULL, // User password. NULL = current 0, // Locale. NULL indicates current WBEM_FLAG_CONNECT_USE_MAX_WAIT, // Security flags 0, // Authority 0, // Context object &pSvc_test // pointer to IWbemServices proxy ); if (FAILED(hres)) { char error_msg[64]; sprintf(error_msg, "wmi_test: Could not connect. Error code = 0x%x", hres); MessageBox(error_msg , "Error" , MB_ICONERROR | MB_OK); pLoc_test->Release(); return; }
Since you are using
WBEM_FLAG_CONNECT_USE_MAX_WAIT
, why not set yourWaitForSingleObject()
call to also wait for 2 minutes (or a little more)?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Since you are using
WBEM_FLAG_CONNECT_USE_MAX_WAIT
, why not set yourWaitForSingleObject()
call to also wait for 2 minutes (or a little more)?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
i am scanning alot of hosts with this app, waiting 2mins for every host would take for ever to scan the network, hence i use the thread timeout of 3secs i just read about _endthread and it appears to work so far. is this a proper solution?
-
i am scanning alot of hosts with this app, waiting 2mins for every host would take for ever to scan the network, hence i use the thread timeout of 3secs i just read about _endthread and it appears to work so far. is this a proper solution?
viperlogic wrote:
i am scanning alot of hosts with this app, waiting 2mins for every host would take for ever to scan the network, hence i use the thread timeout of 3secs
I did something very similar to this in a ping-type application. For each machine that I was going to ping, I just created a secondary thread and put the ping-related code in it. Given that the machines each responded in their own time, the threads all completed at different times; some were instantaneous while others took a lot longer. If your UI has some sort of listbox or list control for these machines, give them a default status of "Establishing connection. Please wait...". When that machine's thread terminates, replace the default status with what actually happened.
viperlogic wrote:
i just read about _endthread and it appears to work so far. is this a proper solution?
Not unless you also used
_beginthread()
. Regardless, it is called from within the secondary thread. Terminating a thread from within itself is vastly different than terminating a thread from some other thread.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb