zoukankan      html  css  js  c++  java
  • windowService中使用多线程

    windowService中使用多线程

    代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BusinessServices;
    using Common.Entities;
    using System.Configuration;
    using System.Data;
    using OrderMaster;
    using System.Collections;
    using System.Threading;

    namespace DownloadItemEmialWinService
    {
    public class EmailSendManager
    {
    public static void SendEmail(string path, string fromEmail, string Smtp, string userName, string password)
    {

    //需要发送记录
    List<DownloadItemLog> ListDownloadItemLog = DownloadItemLogManager.DownloadItemLogSendLogs();
    List<ProcessDownLoadItemLine> pols = new List<ProcessDownLoadItemLine>();

    foreach (DownloadItemLog item in ListDownloadItemLog)
    {
    ProcessDownLoadItemLine pol = new ProcessDownLoadItemLine(item,path,fromEmail,Smtp,userName,password);
    pols.Add(pol);
    }
    ProcessDownLoadItemWithThread(pols);

    }


    /// <summary>
    /// 根据分类Id查找当前分类下的所有原料信息
    /// </summary>
    /// <param name="ee"></param>
    /// <param name="categroyCode"></param>
    /// <returns></returns>
    public static List<ApprovedPurchasePrice> GetItemByCategroyCode(ExcelEdit ee, string categroyCode, string categroyName, List<ApprovedPurchasePrice> appItem)
    {
    List<ApprovedPurchasePrice> itemList = appItem.FindAll(c => c.Item_Class_I == categroyCode);
    ee.CreateWorkSheet(categroyName);
    ee.WriteData(EblastToXls(itemList), 1, 1);
    return itemList;
    }


    /// <summary>
    /// 根据categroyCode生成对应的原料信息
    /// </summary>
    /// <param name="appItems"></param>
    /// <returns></returns>
    public static DataTable EblastToXls(List<ApprovedPurchasePrice> appItems)
    {
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Item_No", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("Item_Name", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("Purchase_Unit_Of_Measure_Name", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("Item_Specification", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("StartDate", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("EndDate", Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("Unit_Price", Type.GetType("System.String")));

    DataRow drTitle = dt.NewRow();
    drTitle["Item_No"] = "原料编号";
    drTitle["Item_Name"] = "原料名称";
    drTitle["Purchase_Unit_Of_Measure_Name"] = "单位";
    drTitle["Item_Specification"] = "原料规格";
    drTitle["StartDate"] = "开始时间";
    drTitle["EndDate"] = "结束时间";
    drTitle["Unit_Price"] = "单价";
    dt.Rows.Add(drTitle);

    foreach (ApprovedPurchasePrice app in appItems)
    {
    DataRow dr = dt.NewRow();
    dr["Item_No"] = app.Item_No;
    dr["Item_Name"] = app.Item_Name;
    dr["Purchase_Unit_Of_Measure_Name"] = app.Pur_Unit_of_Measure_Name;
    if (!String.IsNullOrEmpty(app.Item_Specification))
    {
    dr["Item_Specification"] = app.Specification;
    }
    DateTime sdate;
    DateTime edate;
    if (app != null)
    {
    sdate = app.Starting_Date;
    edate = app.Ending_Date;
    }
    else
    {
    sdate = DateTime.MaxValue;
    edate = DateTime.MaxValue;
    }
    if (DateTime.Compare(Convert.ToDateTime(app.Starting_Date.ToString("HH:ss")), Convert.ToDateTime("0:00:00")) == 0)
    {
    dr["StartDate"] = app.Starting_Date.ToString("yyyy-MM-dd");
    }
    else
    {
    dr["StartDate"] = app.Starting_Date;
    }
    if (DateTime.Compare(Convert.ToDateTime(app.Ending_Date.ToString("HH:ss")), Convert.ToDateTime("0:00:00")) == 0)
    {
    dr["EndDate"] = app.Ending_Date.ToString("yyyy-MM-dd");
    }
    else
    {
    dr["EndDate"] = app.Ending_Date;
    }
    dr["Unit_Price"] = Utilities.Utility.FormatNumber(app.Unit_Price);


    dt.Rows.Add(dr);
    }
    return dt;
    }

    /// <summary>
    /// 从webService中获得所有的原料信息
    /// </summary>
    /// <param name="startDate"></param>
    /// <returns></returns>
    private static List<ApprovedPurchasePrice> GetItemInfo(DateTime startDate, DateTime endDate,string companyName,string unitCode)
    {
    //base.ResetWebService();
    ApprovedPurchasePrice_Service apps = new ApprovedPurchasePrice_Service(companyName);

    ApprovedPurchasePrice_Filter apfilter = new ApprovedPurchasePrice_Filter();
    apfilter.Field = ApprovedPurchasePrice_Fields.Starting_Date;
    apfilter.Criteria = "<=" + endDate.ToString("MM/dd/yyyy");

    //获取过滤条件(结束时间)
    ApprovedPurchasePrice_Filter apfilter1 = new ApprovedPurchasePrice_Filter();
    apfilter1.Field = ApprovedPurchasePrice_Fields.Ending_Date;
    apfilter1.Criteria = ">=" + startDate.ToString("MM/dd/yyyy");

    ApprovedPurchasePrice_Filter apfilter2 = new ApprovedPurchasePrice_Filter();
    apfilter2.Field = ApprovedPurchasePrice_Fields.Unit_Code;
    apfilter2.Criteria = unitCode;

    ApprovedPurchasePrice[] appList = apps.ReadMultiple(new ApprovedPurchasePrice_Filter[] { apfilter, apfilter1, apfilter2 }, "", 0);
    return appList.ToList();
    }

    /// <summary>
    /// 根据分类Id查找所有分类信息
    /// </summary>
    public class CategroyCodeComparer : IEqualityComparer<ApprovedPurchasePrice>
    {
    public bool Equals(ApprovedPurchasePrice source, ApprovedPurchasePrice dest)
    {
    return source.Item_Class_I == dest.Item_Class_I;
    }

    public int GetHashCode(ApprovedPurchasePrice obj)
    {
    return obj.Item_Class_I.GetHashCode();
    }
    }

    #region 多线程处理
    private static void ProcessDownLoadItemWithThread(List<ProcessDownLoadItemLine> pols)
    {
    int threadCount;
    try
    {
    threadCount = int.Parse(System.Configuration.ConfigurationManager.AppSettings["threadCount"]);
    }
    catch
    {
    threadCount = 1;
    }

    MyThread thread = new MyThread(pols, threadCount);
    System.Threading.ParameterizedThreadStart ptStart = new ParameterizedThreadStart(SubmitOrderToNAV);
    thread.TryExcute(ptStart);
    }


    public class MyThread
    {
    private int _threadCount = 10;
    private int _threadIndex = 0;
    private List<ProcessDownLoadItemLine> _pols = null;

    public MyThread(List<ProcessDownLoadItemLine> pols, int threadCount)
    {
    this._pols = pols;
    this._threadCount = threadCount;
    }

    public void TryExcute(ParameterizedThreadStart ptStart)
    {
    Thread[] threadList = new Thread[_threadCount];

    for (int i = 0; i < _pols.Count; i++)
    {
    if (threadList.Length > i)
    {
    threadList[i] = new Thread(ptStart);
    threadList[i].Start(_pols[i]);
    }
    else
    {
    if (_threadIndex == _threadCount)
    {
    _threadIndex = 0;
    }

    while (threadList[_threadIndex].ThreadState == System.Threading.ThreadState.Running)
    {
    Thread.Sleep(1000);
    }

    threadList[_threadIndex] = new Thread(ptStart);
    threadList[_threadIndex].Start(_pols[i]);
    _threadIndex++;

    }
    }
    }
    }


    private static void SubmitOrderToNAV(Object processOrderLine)
    {
    ProcessDownLoadItemLine pol = (ProcessDownLoadItemLine)processOrderLine;
    bool send_flag = false;

    if (pol!=null&&pol.DownloadItemLog!=null)
    {
    List<ApprovedPurchasePrice> appItem = GetItemInfo(pol.DownloadItemLog.StartDate.Value, pol.DownloadItemLog.EndDate.Value, pol.DownloadItemLog.CompanyName, pol.DownloadItemLog.UnitCode);

    if (appItem.Count != 0)
    {
    List<ApprovedPurchasePrice> appCategroy = appItem.Distinct(new CategroyCodeComparer()).ToList();

    ExcelEdit ee = new ExcelEdit();
    ee.CreateExcel();
    for (int i = appCategroy.Count - 1; i >= 0; i--)
    {
    ApprovedPurchasePrice app = (ApprovedPurchasePrice)appCategroy[i];
    GetItemByCategroyCode(ee, app.Item_Class_I, app.Item_Class_I_Name, appItem);
    }
    ee.DeleteSheet("Sheet1");
    string savePath = pol._path;
    string fileName = pol.DownloadItemLog.UnitCode + pol.DownloadItemLog.StartDate.Value.ToString("yyyy-MM-dd") + pol.DownloadItemLog.EndDate.Value.ToString("yyyy-MM-dd") + ".xls";
    savePath += fileName;
    if (System.IO.File.Exists(savePath))
    {
    System.IO.File.Delete(savePath);
    }
    if (ee.SaveAs(savePath))
    {
    ee.Close();

    ArrayList strList = new ArrayList();
    strList.Add(savePath);

    System.Text.StringBuilder sbContent = new System.Text.StringBuilder();
    sbContent.Append(pol.DownloadItemLog.CompanyName + "<br>");
    sbContent.Append("供应点:" + pol.DownloadItemLog.UnitCode + "<br>");
    sbContent.Append("<br>");
    sbContent.Append("开始时间:" + pol.DownloadItemLog.StartDate + "<br>");
    sbContent.Append("结束时间" + pol.DownloadItemLog.EndDate + "<br>");
    sbContent.Append("<br>");
    sbContent.Append("Thanks.<br>");
    send_flag = Utilities.EmailService.SendEmail(pol._fromEmail, pol.DownloadItemLog.EmailAddress, pol.DownloadItemLog.CompanyName + "/" + pol.DownloadItemLog.StartDate.Value.ToString("yyyy-MM-dd") + "/" + pol.DownloadItemLog.EndDate.Value.ToString("yyyy-MM-dd") + "/下载产品列表", sbContent.ToString(), pol._userName, pol._password, pol._Smtp, strList, "");

    }
    }

    if (send_flag)
    {
    pol.DownloadItemLog.DownloadStatus = 1;
    }
    else
    {
    pol.DownloadItemLog.DownloadStatus = -1;
    }

    pol.DownloadItemLog.ModifyPerson = "EmailService";

    DownloadItemLogManager.DownloadItemLogInsUpd(pol.DownloadItemLog);

    }

    }

    public class ProcessDownLoadItemLine
    {
    public DownloadItemLog DownloadItemLog { get; set; }
    public string _path { get; set; }
    public string _fromEmail { get; set; }
    public string _Smtp { get; set; }
    public string _userName { get; set; }
    public string _password { get; set; }
    public ProcessDownLoadItemLine(DownloadItemLog dil, string path, string fromEmail, string Smtp, string userName, string password)
    {
    DownloadItemLog = dil;
    _path = path;
    _fromEmail = fromEmail;
    _Smtp = Smtp;
    _userName = userName;
    _password = password;
    }
    }

    #endregion

    }
    }

  • 相关阅读:
    一文读懂快速排序
    15道APP测试面试题分享,助攻你的面试
    APP测试之使用ADB可能遇到的错误及解决办法
    APP测试之Monkey压力测试(二)
    APP测试之Monkey压力测试(一)
    APP日志文件抓取及分析
    Linux环境安装python3
    visualvm 插件 visual gc 使用介绍
    设计模式之状态
    【深入理解JVM】:Java内存模型JMM
  • 原文地址:https://www.cnblogs.com/zxtceq/p/7767358.html
Copyright © 2011-2022 走看看