Wednesday, 15 January 2014

c - How to overcome a blocking 3rd party function -



c - How to overcome a blocking 3rd party function -

i using 3rd party functions (hrmcom.dll) in win32 application. have main windowclass winproc , 3 childwindows, of sec windowclass share 1 childproc. phone call 3rd party functions in 1 of childwindows , handled in childproc based on wm_command (a button pushed , functions called).

one of these functions blocking. makes connection irda device , 'hangs' when device not nowadays , in connect mode. wanted take care of situation timeout of call.

what tried phone call settimer (call @ same window blocking function is) before blocking phone call , define wm_timer handle. not working, debugging shows function hangs , wm_timer event never handled. tried phone call timer main window, not working.

i saw working illustration of dll in 1 button used starting , cancelling proces of connection device.

question: how deal hanging function , time out after x seconds when not connecting device? have been reading threads, wondering if can done complicated (i have not been able threads work far).

below header file part of called function, if helps:

__declspec (dllexport) bool callback fnhrmcom_startircommunication (int, lptstr); // homecoming value: // bool bstartok // true - starting of communication made succesfully // false - problems encountered, check next possible errors: // * communication has been started , running // * communication port reserved other device // * maybe phone call made 16-bit program. 32-bit dll cannot // create additional thread when dll beingness called // 16-bit program. // ///////////////////////////////////////////////////////////////////////////////

latest code included: thread function (simplified):

void thread(pvoid pvoid) { volatile pparams pparams; pparams = (pparams) pvoid; if (pparams->bcontinue) { if (!fnhrmcom_resetircommunication(0)) { // resetting ir connection not successful errmsg(l"resetting ir connection not successful"); return; } if (!fnhrmcom_startircommunication(hrmcom_param_irda, l"ir")) { // irda couldn't opened, stop connection thread fnhrmcom_endircommunication(false); errmsg(l"irda couldn't opened, connection thread stopped"); return; } //status study "ir communication started" setwindowtext(hwndctrl[11], l"status: ir communication started"); if (!fnhrmcom_readmonitorinfo(&psg, &psmi)) { // reading failed, close connection if (pparams->bcontinue) { fnhrmcom_endircommunication(false); errmsg(l"reading failed, connection closed01"); sendmessage(hwndctrl[8], lb_resetcontent, 0, 0); return; } else { fnhrmcom_endircommunication(false); errmsg(l"connection aborted, connection closed"); sendmessage(hwndctrl[8], lb_resetcontent, 0, 0); return; } } } // end ir communication fnhrmcom_endircommunication(false); } _endthread(); }

code calling , cancelling thread manually:

case (id_ctrl + 12) : //cancel connect device params.bcontinue = false; if (!fnhrmcom_endircommunication(false)) { //todo: error homecoming 0; } terminatethread(hthread, 0); homecoming 0; case (id_ctrl + 7) : //load activities device params.bcontinue = true; if (idcancel != messagebox(hwnd, l"make sure device set connectmode", l"warning", mb_okcancel | mb_iconexclamation)) { //start downloading activities in separate thread settimer(hwnd, idt_timer1, 10000, null ); hthread = (handle)_beginthread(thread, 0, &params); } //cancelled, dont start irda connection thread homecoming 0;

and code timer handler:

case wm_timer: if (loword(wparam) == idt_timer1) { if (!fnhrmcom_isirdaconnected()) { setwindowtext(hwndctrl[11], l"status: irda connection timed out"); terminatethread(hthread, 0); fnhrmcom_endircommunication(false); killtimer(hwnd, idt_timer1); errmsg(l"irda connection timed out"); homecoming 0; } else killtimer(hwnd, idt_timer1); } homecoming 0;

put blocking phone call it's own thread.

your threading function simple this:

volatile bool success; volatile bool done; void connectthread() { success = false; done = false; if (!fnhrmcom_startircommunication(123, "some stuff")) { std::cerr << "there's been ir problem." << std::endl; done = true; return; } success = true; done = true; }

then timeout outside, waiting signal, in illustration done set. if timeout period on without done beingness set, know it's taking long (and can kill thread), otherwise you're able read success determine whether worked.

c winapi

No comments:

Post a Comment