Block Sock Block

August 7th, 2007 / Join Them

WinHttpGetProxyForUrl also blocks.  This is despite the documentation saying that it won’t if you close the handle during it’s operation.  (Let’s face it, Microsoft programmers often have a very loose sense of the meaning of block, what’s a minute or two.)  Anyway, this called for a generic class for blocking things (see previous post re gethostbyname and getaddrinfo blocking).  Anyway, the class (as before Thread is a pretty basic personal thread class):

class CAbortableAction : public Thread
{
public:
    CAbortableAction() : m_abort(CreateEvent(0,TRUE,FALSE,0),0,CloseHandle) {}
    virtual void AbortAction() {SetEvent(m_abort);}
protected:
    virtual bool StartAction()
    {
        ResetEvent(m_abort);
        if(!Create().Valid())
            return false;
        HANDLE handles[] = {ThreadHandle(),m_abort};
        return (
            WaitForMultipleObjects(sizeof(handles)/sizeof(handles[0]),
                    handles,FALSE,INFINITE)=WAIT_OBJECT_0);
    }
    virtual void DoAction() = 0;
    bool IsAborted() { return EventHandle(m_abort).IsSet();}
private:
    auto_HANDLE m_abort;
    DWORD ThreadProc() {DoAction();return 0;}
};

Comments are closed.