Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在单独的线程上重用连接_C#_Multithreading_Http_Timer_Keep Alive - Fatal编程技术网

C# 在单独的线程上重用连接

C# 在单独的线程上重用连接,c#,multithreading,http,timer,keep-alive,C#,Multithreading,Http,Timer,Keep Alive,我需要听一个开放的API,在正确的数据,我需要下载一些文件和处理它。我使用计时器每x秒自动发出一个下载请求,这使连接保持活动状态,因此可供重用 共有3个组成部分: 父类 侦听器类 计时器 父类在单独的线程上生成侦听器,并启动计时器。计时器和侦听器都调用父对象上的委托 问题是,每次从计时器下载时,连接都会被重用(从下载时间可以明显看出),但当侦听器调用相同的连接时,连接就不会被重用 裸骨代码: using Timer = System.Timers.Timer; namespace Hallel

我需要听一个开放的API,在正确的数据,我需要下载一些文件和处理它。我使用计时器每x秒自动发出一个下载请求,这使连接保持活动状态,因此可供重用

共有3个组成部分:

  • 父类
  • 侦听器类
  • 计时器
  • 父类在单独的线程上生成侦听器,并启动计时器。计时器和侦听器都调用父对象上的委托

    问题是,每次从计时器下载时,连接都会被重用(从下载时间可以明显看出),但当侦听器调用相同的连接时,连接就不会被重用

    裸骨代码:

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

    知道为什么会这样吗?

    当计时器事件被调用时,您将创建一个新的保持活动请求


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

    我已编辑了您的标题。请看“”,其中的共识是“不,他们不应该”。谢谢!迈向成为一个更好的网民!