线程定时器

首先我们需要定义一个定时器,在固定时间创建一个新的线程

//定时器建立新线程 避免主线程阻塞
            var timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(2);
            timer.Tick += new EventHandler(delegate(Object o, EventArgs a)
            {
                m_BackgroundWorker = new BackgroundWorker();
                m_BackgroundWorker.DoWork += new DoWorkEventHandler(getMsg);
                m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(processShoot);
                m_BackgroundWorker.RunWorkerAsync(this);
                
            });
            timer.Start();

然后定义新线程要做什么事

 //获取弹幕数据
        public void getMsg(object sender, DoWorkEventArgs e)
        {
            try
            {
                //WebRequest wReq = WebRequest.Create("http://192.168.5.30:20080/?r=test/weixin");
                WebRequest wReq = WebRequest.Create("http://104.214.140.145/qywx/web/index.php?r=annual/default/barrage");
                WebResponse wResp = wReq.GetResponse();
                Stream respStream = wResp.GetResponseStream();
                using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding("UTF-8")))
                {
                    String fileData = reader.ReadToEnd();
                    if(fileData != ""){
                        String[] dataRows = fileData.Split(new string[] { ";;;" }, StringSplitOptions.None);
                        //一个时间单位最多只获取10条弹幕

                        String[,] result = new String[10, 5];
                        int j = 0;
                        for (int i = 0; i < 10; i++)
                        {
                            try
                            {
                                String[] oneData = dataRows[i].Split(new string[] { ":::" }, StringSplitOptions.None);
                                if (string.IsNullOrEmpty(oneData[0]) == true) continue;
                                if (string.IsNullOrEmpty(oneData[1]) == true) continue;
                                result[j, 0] = oneData[0];
                                result[j, 1] = oneData[1];
                                result[j, 2] = oneData[2];
                                result[j, 3] = oneData[3];
                                result[j, 4] = oneData[4];
                                j++;
                            }
                            catch (System.Exception ex)
                            {
                                continue;
                            }
                        }
                        e.Result = result;
                        
                    }
                    
                }
            }
            catch (System.Exception ex)
           {
           }
        }

最后在主线程处理返回的数据

//处理异步返回的数据
        public void processShoot(object sender, RunWorkerCompletedEventArgs e)
        {
            try
            {

                String[,] fileData = (String[,])e.Result;
                for (int i = 0; i <= fileData.Length; i++)
                {
                    try
                    {
                        shoot(fileData[i, 1], fileData[i, 0], fileData[i, 2], fileData[i, 3], fileData[i, 4]);
                    }
                    catch (System.Exception ex)
                    {
                        break;
                    }
                }

            }
            catch (System.Exception ex)
            {
            }
            
        }