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和操作系统(也称为“主线程”)自动创建的,并具有多 ...
随机推荐
- 非常酷的word技巧---删除行前的空格
今天整理一篇文章的时间遇见一个问题,非常多行前的空格严重影响美观.搞计算机的就是爱折腾.于是做了各种尝试完美解决,以下把方法发布例如以下,事实上非常easy哦! 问题例如以下情况所看到的: 解决的方法 ...
- 导出txt格式的说明书
/// <summary> /// 说明书 /// </summary> /// <returns></returns> public FileResu ...
- web 表单方式上传文件方法(不用flash插件)
原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- ruby 作为嵌入脚本时使用的注意事项
近期一直在採坑... 假设是作为嵌入式脚本使用ruby的话... 一定会遇到这2个问题... gem安装的,无法在嵌入时使用..为啥.? 由于你没require 'ruby gem' 出现 找不到 E ...
- java集合归纳
学习自: http://android.blog.51cto.com/268543/400557/ MAP Collection 堆栈队列操作尽可能考虑 linkedlist 多线程同步操作尽可能考虑 ...
- Html简单的整页切换
恩,语言组织不是很好,直接上代码吧.... <!DOCTYPE html> <html> <head lang="en"> <meta c ...
- 果园里有一堆苹果,一共n头(n大于1小于9)熊来分,第一头为小东,它把苹果均分n份后,多出了一个,它扔掉了这一个,拿走了自己的一份苹果,接着第二头熊重复这一过程,即先均分n份,扔掉一个然后拿走一份,以此类推直到最后一头熊都是这样(最后一头熊扔掉后可以拿走0个,也算是n份均分)。问最初这堆苹果最少有多少个。
include "stdafx.h" // ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include<iostream> ...
- HDFS源码分析之数据块Block、副本Replica
我们知道,HDFS中的文件是由数据块Block组成的,并且为了提高容错性,每个数据块Block都会在不同数据节点DataNode上有若干副本Replica.那么,什么是Block?什么又是Replic ...
- golang 内存池
一般来说,内存池都是采用预分配的方式,分为固定大小的和非固定大小块,固定大小的内存效率高,非固定大小灵活.同时,分为单线程和多线程版的,单线程不需要考虑并发问题. 一般内存池的实现思想:分配一块比较大 ...