开发者

WebClient force Timeout

开发者 https://www.devze.com 2023-03-15 03:35 出处:网络
public class MyWebClient : WebClient { private int timeout; public int Timeout { get { return timeout; } set {
    public class MyWebClient : WebClient {

        private int timeout;
        public int Timeout
        {
            get
            {
                return timeout;
            }
            set
            {
         开发者_Go百科       timeout = value;
            }
        }

        public MyWebClient()
        {
            this.timeout = 5000;
        }

        public MyWebClient(int timeout)
        {
            this.timeout = timeout;
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var result = base.GetWebRequest(address);
            result.Timeout = this.timeout;
            return result;
        } 
}

I am trying to force timeout to 5000 milliseconds, but it is not working the download does not stop or exit after 5000 milsecs.

It can be done by Task timeout, but i don't want to use Task here.

any alternative way to do it??


Can you give any more information? What are you trying to download from, and are you certain it's not working?

I've tried your code and it seems to work fine; I get a System.Net.WebException "The operation has timed out" - you're definitely not swallowing this exception?


Could you put the request on a new thread, then watch that thread, if you hit your specified timeout then kill the thread thus aborting the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消