-2

How can I catch the timeout exception in a third dll function,I use c language in Windows

I want to catch a timeout Exception while call a thirdly dll function, you know the function takes a long while, and I need it return a value in limited time, if it doesn't return in the time, I will give it a default value.

I have to look for so much infomation about but it doesn't work. I get the two point:

1.use the alarm function in ,but it only work in Linux,I can't use it in Windows even I use the MinGW standerd GCC complier.

2.use the timeSetEvent function in and the setjmp/longjmp function in ,the three function maybe so closed to take it work.but I use them caused the programe dump,windows pops a DialogMessage say something wrong.

I give the code and the picture like this :

`

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setjmp.h>

jmp_buf j;

/**
 * 时间中断函数
 */
void PASCAL OneMilliSecondProc(UINT wTimerID, UINT msg, DWORD dwUser, DWORD dwl, DWORD dw2) {
    printf("Timout!\n");
    longjmp(j,1);
}

int longTimeFunction(){
    while (1) {
        printf("operating...\n");
        Sleep(1000);
    }

    return 0;
}


int main(){
    HANDLE hHandle;

    UINT wTimerRes_1ms;//定义时间间隔
    UINT wAccuracy; //定义分辨率
    UINT TimerID_1ms; //定义定时器句柄
    wTimerRes_1ms = 5000;
    if((TimerID_1ms = timeSetEvent(
                          wTimerRes_1ms,
                          wAccuracy,
                          (LPTIMECALLBACK)OneMilliSecondProc, // 回调函数
                          (DWORD)(1), // 用户传送到回调函数的数据;
                          TIME_PERIODIC//周期调用定时处理函数
                      )) == 0) {
        printf("start!!!!!!!!!!!\n");
    } else {
        printf("end!!!!!!!!!!!\n");
    }

    int temp = 0;
    if(setjmp(j) == 0){
        temp = longTimeFunction();
    }else{
        printf("xxxxxx...\n");
        temp = -1;
    }

    printf("%d\n", temp);

    return 0;
}

` enter image description here

Revolver_Ocelot
  • 7,747
  • 3
  • 26
  • 45

1 Answers1

0

Unlike UNIX signals, timeSetEvent doesn't interrupt a thread, the callback runs in parallel and longjmping across threads is undefined behavior.

Concerning your actual question, this is a bad idea. Such an abortion could leave the library in an inconsistent state.

Instead, try to get the library vendor to offer an API that accepts a timeout, or use another library that already supports it.

a3f
  • 7,797
  • 1
  • 33
  • 39
  • How about that:```HANDLE longTimeFunctionHandle = CreateThread(NULL, 0, longTimeFunction, NULL, 0, NULL); WaitForSingleObject(longTimeFunctionHandle, 200);``` – newflypig Aug 02 '16 at 02:16
  • @newflypig [Read this](http://stackoverflow.com/questions/4149146/why-and-when-shouldnt-i-kill-a-thread/4149210#4149210). If there is really no way around calling `TerminateThread`, disassemble the function and check that aborting is safe. What does the function do? – a3f Aug 02 '16 at 07:13
  • the function try to connect to a CNC machine with ethernet. – newflypig Oct 14 '16 at 02:57