在单独的线程上重用连接

时间:2013-11-14 02:56:01

标签: c# multithreading http timer keep-alive

我需要听一个开放的API,根据正确的数据我需要下载一些文件并进行处理。我正在使用计时器每隔x秒自动发出一次下载请求,这样可以保持连接的有效性,因此可以重复使用。

总共有3个组成部分:

  1. 父类
  2. 听众班
  3. 定时器
  4. 父类在单独的线程上生成侦听器,并启动计时器。计时器和侦听器都会调用父节点上的委托。

    问题在于,每次从计时器下载时都会重复使用连接(从下载时间来看很明显),但是当监听器调用连接时,它不是。

    裸骨代码:

    using Timer = System.Timers.Timer;
    
    namespace Hallelujah
    {
        public delegate void downloadData(bool process, string url);
        public delegate void downloadDataInvoker(bool process, string url);
    
    class Listener2
    {
        downloadData _downloadData;
        public void start(string storeID, string shoes)
        {
            ServicePointManager.DefaultConnectionLimit = 500;
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.MaxServicePoints = 0;
    
            _downloadData += new downloadData(downloadDataCb);
    
            Timer timer = new Timer(10000);
            timer.Elapsed += timer_Elapsed;
            timer.Enabled = true;
    
            Listen listenTool = new Listen();
            listenTool._downloadData += new downloadDataInvoker(invoker);
    
            Thread thread = new Thread(new ParameterizedThreadStart(listenTool.init));
            string[] str = { params };
            thread.Start(str);
    
            F1.updateStatus(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
        }
    
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _downloadData.Invoke(false, "example.com");
        }
    
        public void invoker(bool process, string url)
        {
            _downloadData.Invoke(process, url);
        }
        public void downloadDataCb(bool process, string url)
        {
            F1.updateStatus(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.KeepAlive = true;
            req.ProtocolVersion = HttpVersion.Version10;
            req.UnsafeAuthenticatedConnectionSharing = true;
            req.Pipelined = true;
            WebResponse res = req.GetResponse();
            Stream sr = res.GetResponseStream();
            //Reading response
            if (process)
            {
                F1.updateStatus("Downloaded  in " + t1.Seconds + " s " + t1.Milliseconds + "ms ");
            }
            else
                F1.updateStatus("Downloaded NULL(from timer) in " + t1.Seconds + " s " + t1.Milliseconds + "ms ");
        }
    
        public void updateStatusCB(string status)
        {
            F1.updateStatus(status);
        }
    
    }
    internal class Listen
    {
        public downloadDataInvoker _downloadDataInvoker;
        public updateStatus _updateStatus;
        public bool ToListen = true;
    
        public void init(object objpass)
        {
            _updateStatus.Invoke(System.Threading.Thread.CurrentThread.ManagedThreadId.ToString());
    
             while(ToListen){
                //When a match is found
                _downloadDataInvoker.Invoke(true, media_url);
            }
    
    
        }
    }
    

    知道为什么会这样发生?

1 个答案:

答案 0 :(得分:0)

在调用计时器事件时创建一个新的保持活动请求。

根据您的需要,有很多下载任务(通过user_id?),您可以为每个任务创建一个保持活动请求,并开始请求异步,在回调事件中调用更新委托。

相关问题