我在桌面程序中有一個業務邏輯處理。Web服務中的多線程以加快處理速度
然而,數據處理是可怕的慢,當我與大數據,(我使用MS SQL數據庫)處理。因此,我會做一些工作。我打開Web服務項目,並將其打包我的業務邏輯處理成一個DLL名稱consoleAttn(),則處理如下代碼分成幾個線程,與DOTNET的Task.Parallel理工科:
`
DataTable dtEmp = new DataTable("Employee");
if (cn.State == ConnectionState.Closed)
{
cn.Open();
}
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sEmpQuery, cn))
{
Console.Write(string.Format("Command Text:{0}", sEmpQuery));
cmd.Parameters.AddWithValue("Period", sPeriod);
dtEmp.Load(cmd.ExecuteReader());
}
if (cn.State == ConnectionState.Open)
{
cn.Close();
}
Int32 iThread;
iThread = iNoThread;
Int32 iCountTable;
iCountTable = dtEmp.Rows.Count;
Console.WriteLine(string.Format("Total Rows:{0}", iCountTable));
Int32 IProcNum;
IProcNum = iCountTable/iThread;
Console.WriteLine(string.Format("Transaction no:{0}", IProcNum));
if (iCountTable < iThread)
{
Boolean bl;
AutoHRPay.frmClosePeriod aPay= new AutoHRPay.frmClosePeriod();
MiscResource AMisc = new MiscResource();
MiscResource misc = new MiscResource();
misc.SetDBConn(strCN);
}
else
{
Boolean[] bl = new Boolean[iThread];
object locker = new object();
Parallel.For(1, iThread + 1, i =>
{
System.Text.StringBuilder strSQL = new System.Text.StringBuilder();
strSQL.Append("(");
DataTable dtEmpThread = dtEmp.Clone();
dtEmpThread.Clear();
for (Int32 j = ((i - 1) * IProcNum + 1); j <= ((i != iThread) ? (IProcNum * i) : iCountTable); j++)
{
dtEmpThread.ImportRow(dtEmp.Rows[j-1]);
}
int row_counter = 0;
lock (locker)
{
row_counter++;
streamwriter.WriteLine("Processing row: {0}", row_counter);
}
ConsoleAttn aPay = new ConsoleAttn();
aPay.runConsole();
});
}
streamwriter.Close();
return "Completed.";
1)你有什麼想法,當我需要停止進程或調用Web服務的客戶端崩潰時,我應該如何執行異步過程?