How to abort a HttpWebRequest (from another thread)?

165 Views Asked by At

I like to abort HttpWebRequest when necessary from another thread. Is it right way to do it ?

    public void Abort()
    {
        if (request != null)
        {
            try { request.ReadWriteTimeout = 0; } catch { }
            try { request.Timeout = 0; } catch { }
            try { request.Abort(); } catch { }
        }
    }
1

There are 1 best solutions below

0
On

I believe it will save you some effort to put everything inside one try/catch. Also look at this: Is it possible to abort a Task like aborting a Thread (Thread.Abort method)?

public void Abort()
{
    if (request != null)
    {
        try 
        { 
            request.ReadWriteTimeout = 0; } catch { }
            request.Timeout = 0;
            request.Abort(); 
        } catch {//Some error}
    }
}