Block Sock Block

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;}
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s