1、前几天做数据导出,导出时几十个业务号数据导出串行处理,所有业务号处理完毕花费时间长。
2、想改进处理,多个业务号并行处理,于是想到了多线程,业务处理调用
外网 webservice,多线程启动时请求出错。
3、改进程序处理,把原来线程中的处理放在进程中,启动都个进程。
这里是启动操作
static void Main(string[] args)
{
string[] businessTypeArray = { "03", "04", "18", "01", "02", "05", "06", "11", "12", "13", "14", "15", "16", "17", "21", "22", "23", "62", "31", "32", "33", "41", "51", "52", "88", "601", "602", "603", "604", "605", "606", "607", "608", "609", "610", "701", "702" };
for (int i = 0; i < businessTypeArray.Length; i++)
{
exportProcess objExportProcess = new exportProcess(businessTypeArray[i]);
ThreadStart startExport = new ThreadStart(objExportProcess.excuteExport);
Thread thredExport = new Thread(startExport);
thredExport.Start();
}
}
这里是task调用处理
public class exportProcess
{
private string bussinessType;
public exportProcess(string bussinessType)
{
this.bussinessType = bussinessType;
}
public void excuteExport()
{
var objExportTask = new Task<outPutExport>(exportTask, new inPutExport() { businessTypeInput = bussinessType });
objExportTask.Start();
}
}
#endregion
#region task 调用
public class inPutExport
{
private string businessType;
public string businessTypeInput { get => businessType; set => businessType = value; }
}
public class outPutExport
{
private string outPut;
public string OutPut { get => outPut; set => outPut = value; }
}
public static outPutExport exportTask(object para)
{
return new outPutExport()
{
OutPut = Export.exportData(((inPutExport)para).businessTypeInput)
};
}
这里是cmd启动进程处理
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Diagnostics;
namespace exportTool
{
public class Export
{
#region 导出
public static string exportData(string businessType)
{
string strCmd = "";
strCmd = "exportFromWeb " + businessType;
string strOutPut = ExecuteExport(strCmd, 30000);
return strOutPut;
}
#endregion
#region 执行DOS命令,等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待,返回DOS命令的输出,如果发生异常,返回空字符串
public static string ExecuteExport(string dosCommand, int milliseconds)
{
string output = ""; //输出字符串
if (dosCommand != null && dosCommand != "")
{
Process process = new Process(); //创建进程对象
process.StartInfo.FileName = "cmd.exe"; //设定需要执行的命令
process.StartInfo.UseShellExecute = false; //不使用系统外壳程序启动
process.StartInfo.RedirectStandardInput = true; //不重定向输入
process.StartInfo.RedirectStandardOutput = true; //重定向输出,而不是默认的显示在dos控制台
process.StartInfo.RedirectStandardError = true; //输出错误信息
process.StartInfo.CreateNoWindow = true; //不创建窗口
process.StartInfo.Arguments = "/C " + dosCommand; //设定参数,其中的“/C”表示执行完命令后马上退出
try
{
if (process.Start()) //开始进程
{
if (milliseconds == 0)
process.WaitForExit(); //这里无限等待进程结束
else
process.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒
output = process.StandardOutput.ReadToEnd();//读取进程的输出结果。
if (process.ExitCode == 0)
{
return "export success";
}
else {
return "export Error";
}
}
}
catch (Exception ex)
{
output = ex.Message;
}
finally
{
if (process != null)
{
process.Close();
process.Dispose();
}
}
}
return output;
}
#endregion
}
}