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和操作系统(也称为“主线程”)自动创建的,并具有多 ...
随机推荐
- 【剑指Offer学习】【面试题37:两个链表的第一个公共结点】
题目:输入两个链表,找出它们的第一个公共结点. 链表结点定义 /** * 链表结点类 */ private static class ListNode { int val; ListNode next ...
- servlet实现文件下载所需步骤及说明
servlet实现文件下载所需步骤及说明 CreateTime--2017年9月1日15:46:22 Author:Marydon 参考链接:http://blog.sina.com.cn/s/b ...
- SW线路中串联1K电阻的作用
主要作用的去ESD,去干扰 和ADC上串的一样一样的作用 物美价廉的ESD方案 我还是觉得起到控制开关打开的快慢,也就是控制开关脉冲的上升沿,加了这个1K电阻,可以减缓上升延的斜率.如果上升沿过快,会 ...
- wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql
注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...
- mapreduce代码实现入门
mapreduce代码主要包括三个类,map类.reduce类以及测试类! 以wordcount为例, map类为: static class WordMapper extends Mapper< ...
- 【BZOJ3924】[Zjoi2015]幻想乡战略游戏 动态树分治
[BZOJ3924][Zjoi2015]幻想乡战略游戏 Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网 ...
- 网页直播、微信直播技术解决方案:EasyNVR与EasyDSS流媒体服务器组合之区分不同场景下的easynvr
近期遇到好多客户咨询关于实现微信直播.或者是将直播页面集成进入自己项目中. 该方案的主要目的:完成在公网一直进行内网摄像头的RTMP/HLS直播! 实现方案的具体实现: EasyNVR+EasyDSS ...
- 在做RTSP摄像机H5无插件直播中遇到的对接海康摄像机发送OPTIONS心跳时遇到的坑
我们在实现一套EasyNVR无插件直播方案时,选择了采用厂家无关化的通用协议RTSP/Onvif接入摄像机IPC/NVR设备,总所周知,Onvif是摄像机的发现与控制管理协议,Onvif用到的流媒体协 ...
- 发送邮件 Email(java实现)
//发送邮件 private static void sendMail(String mail, String mailContext) { try { //获取文本中html的内容 并动态替换并显示 ...
- 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题
https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.3.md 9.3 锁和 sync 包 在一些复杂的程序中,通常通 ...