各位大侠,我要实现这样一个功能,在DLL中调用一个函数,该函数声明如下:typedef void (__stdcall *Error_CallBack)(const CNF_ERR_CODE  mr);
int  __stdcall Init_r(const int meta_handle, const Error_CallBack  cb);我想通过以下类似的函数对以上函数进行封装int Init(const int meta_handle, int timeout)以上都是DLL内的函数,要求DLL是线程安全的。用户EXE调用Init,最长等待timeout毫秒,即可返回成功或失败。请问通过什么方式实现比较好,通过定时器SetTimer?请各位大侠指教!

解决方案 »

  1.   

    直接阻塞不就可以了?
    返回TRUE / FALSE
    或是事件等待来实现超时
      

  2.   

    是否在Init函数内做个循环,等待一个标志变量的改变,如果回调函数被调用,标志变量被改变,则退出__declspec( thread ) bool tls_i = false; 但是如何才能让Init的循环函数尽量不占CPU时间?我要求精度不高,秒级就可以。那位大侠给出一段代码,谢谢啦!
      

  3.   

    另外,能不能用waitforsingleobject来完成这个功能?
      

  4.   

    用waitforsingleobject来等待事件吧。在你的函数中,如果调用了就把事件SetEvent,等到了后在把事件ResetEvent.这样做简单些。
      

  5.   

        //
        // wait tspprinter until it's ready within timeout.  11h,1ah ? 
        //
        do {
            // delay 400 ms
            WaitForSingleObject(GetCurrentThread(),POOLIVL);
            
            EnterCriticalSection(&cs);
            RtlCopyMemory(lpshot,lpShot,BUF1);
            LeaveCriticalSection(&cs);        bPrint=( lpshot[2]==0x11 || lpshot[2]==0x1a );
            timeout+=POOLIVL;    } while( !bPrint && timeout<nTimeOut );    // timeout ?
        if( timeout>nTimeOut ) 
        {
            code=0x01;
            goto clear;
        }    // can print ?
        if( !bPrint )
        {
            code=lpshot[2];
            goto clear;
        }
        
       // print 
      

  6.   

    我的DLL文件
    ------------------------------
    .h
    typedef void (__stdcall *META_Error_CallBack2)(const int mr);
    int __stdcall META_Init2(const META_Error_CallBack2 cb);
    void __stdcall cbfunc(const int mr);//userextern "C" { class CCalibrate
    {
    public:
    __declspec(dllexport) int testcallback(int timeout);
    private:
    };
    }
    ------------------------------
     .cpp
    ULONG WINAPI thread1(LPVOID param);
    META_Error_CallBack2 gcallback=0;
    bool Meta_Init2_request;
    //_declspec(thread) bool Meta_Init2_request;
    int CCalibrate::testcallback(int timeout)
    {
    int vi;
    if(Meta_Init2_request)return 2;
    vi = META_Init2(::cbfunc); int   step=1;
    DWORD starttick;
    starttick   =  GetTickCount();
    while ((GetTickCount()-starttick) < (DWORD)timeout)
    {   
    if(Meta_Init2_request)break;
    Sleep(100);
    }    //Sleep(5000); if(Meta_Init2_request)
    return 1+timeout;
    else
    return 0+timeout;}int __stdcall META_Init2(const META_Error_CallBack2 cb)
    {
    gcallback=cb;
    DWORD   threadid=0;   
    HANDLE   hthread   = CreateThread(
    NULL,   
    NULL,   
    thread1,   
    LPVOID(0),
    DWORD(0),
    &threadid   
    );   
    return 0;
    }ULONG WINAPI thread1(LPVOID param)
    {
    Sleep(1000);
    (*gcallback)((int)1);
    return 0;
    }void __stdcall cbfunc(const int mr)//回调
    {
    Meta_Init2_request = true;
    }------------------------------但是在EXE调用该DLL testcallback时,Meta_Init2_request变量值会受其他线程影响应该怎样声明这个变量,才能在EXE每一次调用时,该变量不受影响
    _declspec(thread) bool Meta_Init2_request;
    这样也不行分不够我可以新开帖!请大家帮我