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
}
}
windowService中使用多线程的更多相关文章
- 细说.NET 中的多线程 (一 概念)
为什么使用多线程 使用户界面能够随时相应用户输入 当某个应用程序在进行大量运算时候,为了保证应用程序能够随时相应客户的输入,这个时候我们往往需要让大量运算和相应用户输入这两个行为在不同的线程中进行. ...
- 细说.NET中的多线程 (二 线程池)
上一章我们了解到,由于线程的创建,销毁都是需要耗费大量资源和时间的,开发者应该非常节约的使用线程资源.最好的办法是使用线程池,线程池能够避免当前进行中大量的线程导致操作系统不停的进行线程切换,当线程数 ...
- [转载]ArcGIS Engine 中的多线程使用
ArcGIS Engine 中的多线程使用 原文链接 http://anshien.blog.163.com/blog/static/169966308201082441114173/ 一直都想写 ...
- python中的多线程【转】
转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...
- 拒绝卡顿——在WPF中使用多线程更新UI
原文:拒绝卡顿--在WPF中使用多线程更新UI 有经验的程序员们都知道:不能在UI线程上进行耗时操作,那样会造成界面卡顿,如下就是一个简单的示例: public partial class MainW ...
- java中的多线程——进度2
package src;/*多线程总结:1,进程和线程的概念. |--进程: |--线程:2,jvm中的多线程体现. |--主线程,垃圾回收线程,自定义线程.以及他们运行的代码的位置 ...
- Qt中的多线程编程
http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...
- 转:MFC中创建多线程
MFC中创建多线程 MFC的多线程函数必须声明为静态的或者是全局函数(不同的在于全局函数不能访问类的私有静态成员,而静态类函数可以):但这样的线程函数只能访问静态的成员变量,要实现访问类的其他成员 ...
- C#中的多线程-入门
概述与概念C#支持通过多线程并行地执行代码,一个线程有它独立的执行路径,能够与其它的线程同时地运行.一个C#程序开始于一个单线程,这个单线程是被CLR和操作系统(也称为“主线程”)自动创建的,并具有多 ...
随机推荐
- 【Excle数据透视表】如何快速选取所有标签并标注黄色底纹
如下图:需要把所有标签标注为黄色底纹该如何操作呢? 步骤 单击数据透视表任意单元格→数据透视表工具→分析→选择→整个数据透视表→选择→标签→开始→字体组合中"填充颜色" 第一次选择 ...
- No bean named 'cxf' is defined
1.错误描写叙述 严重:Exception starting filter CXFServlet org.springframework.beans.factory.NoSuchBea ...
- Spark Streaming和Kafka整合开发指南(一)
Apache Kafka是一个分布式的消息发布-订阅系统.可以说,任何实时大数据处理工具缺少与Kafka整合都是不完整的.本文将介绍如何使用Spark Streaming从Kafka中接收数据,这里将 ...
- 开源项目WebImageView载入图片
项目地址:https://github.com/ZaBlanc/WebImageView 作者对载入图片,以及图片的内存缓存和磁盘缓存做了封装. 代码量不多.可是可以满足一般的载入图片. 先看下项目结 ...
- 查看并修改Linux主机名命令hostname
查看主机名 hostname可以查看主机名 export也可以查看 修改主机名 echo new-hostname > /proc/sys/kernel/hostname (系统启动时,从此文件 ...
- 说明sizeof和strlen之间的区别。
解析:由以下几个例子我们说明sizeof和strlen之间的区别.第1个例子: sizeof(ss)结果为4,ss是指向字符串常量的字符指针.sizeof(*ss)结果为1,*ss是第一个字符.第2个 ...
- python 基础 7.1 datetime 获得时间
一 datatime 的使用 object timedeta tzinfo time data dat ...
- href=http:// href=// 的区别,src=http:// src=// 的区别。 链接里不带http,链接里直接使用双斜线 // 有什么不同。http://和//有什么区别?
其实很简单,当一个连接用双斜线 // 开头时表示如果浏览器当前使用的是https协议,那么就加载https协议的脚本,否则使用http,这保证了页面所有资源使用同一协议. 其实是有人将其做为规范来实践 ...
- Python --- Scrapy 命令(转)
Scrapy 命令 分为两种: 全局命令 和 项目命令 . 全局命令:在哪里都能使用. 项目命令:必须在爬虫项目里面才能使用. 全局命令 C:\Users\AOBO>scrapy -h Scra ...
- 流畅python学习笔记:第二十章:属性描述符:
在前面一章中介绍了@property的用法,但是存在一个问题,如果我有多个属性想转变成property特性,那不是针对每个都需要实现一个 @propery.setter 和 @property.get ...