Windows服务多线程同步淘宝房源

来源:互联网 发布:交换机数据转发原理 编辑:程序博客网 时间:2024/04/23 16:06

Windows服务多线程同步淘宝房源

        /// <summary>        /// 多线程发布房源信息        /// </summary>        public void SyncHouse()        {            try            {                if (listEst != null)                {                    int avg = listEst.Count / 2;                    start1 = 0;                    end1 = avg;                    start2 = avg;                    end2 = listEst.Count;                }                Thread thread1 = new Thread(new ThreadStart(ToTBProcedure1));                thread1.Start();                Thread thread2 = new Thread(new ThreadStart(ToTBProcedure2));                thread2.Start();            }            catch (Exception ex)            {                LogHelper.WriteLog(typeof(HouseHelper), "ExecMoreProcedure()方法里发生异常!错误原因:" + ex.Message);            }        }

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;namespace Taobao.BJWinService{    public partial class Service1 : ServiceBase    {        System.Timers.Timer time;        public Service1()        {            InitializeComponent();        }        protected override void OnStart(string[] args)        {            time = new System.Timers.Timer();            time.Interval = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["IntervalTime"].ToString()) * 1000 * 60 * 60;// ;//1小时执行一次            time.Elapsed += new System.Timers.ElapsedEventHandler(Time_Elapsed);            time.AutoReset = true;//设置是执行一次(false)还是一直执行(true);                time.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;            }        protected override void OnStop()        {            time.Enabled = false;        }        private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)        {            try            {                //第一步:同步小区数据                EstHelper eh = new EstHelper();                DateTime t1 = DateTime.Now;                LogHelper.WriteLog(typeof(Service1), "同步小区数据开始,开始时间为:" + t1.ToString());                eh.SyncEst();                DateTime t2 = DateTime.Now;                TimeSpan ts = t2 - t1;                LogHelper.WriteLog(typeof(Service1), "同步小区数据结束,结束时间为:" + t2.ToString() + "。总计用时为:" + ts.ToString());                //第二步:同步房源数据                HouseHelper hh = new HouseHelper();                t1 = DateTime.Now;                LogHelper.WriteLog(typeof(Service1), "淘宝推送开始,开始时间为:" + t1.ToString());                hh.SyncHouse();                t2 = DateTime.Now;                ts = t2 - t1;                LogHelper.WriteLog(typeof(Service1), "淘宝推送结束,结束时间为:" + t2.ToString() + "。总计用时为:" + ts.ToString());            }            catch (Exception ex)            {                LogHelper.WriteLog(typeof(Service1), "执行失败!错误:" + ex.Message);            }        }    }}




0 0