public partial class MultiThread : Form
{
List<string> DTtype = new List<string>()
{
"D","F"
};
public MultiThread()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataSet dsDomestic = new DataSet(); //56 ROWS
DataSet dsForeign = new DataSet(); //21 ROWS
int domestic = 56; //假設要執行的資料庫的 DataSet 筆數, 要先排序再做 RowNumber()
int foreign = 21; //假設要執行的資料庫的 DataSet 筆數, 要先排序再做 RowNumber()
int cycle = 10; //假設每次每一執行緒,執行10筆, 若跑不動,則減少 9 ,8 , 7...
int domesticCycle = 0, foreignCycle = 0; //計算要執行的次數
domesticCycle = (domestic % cycle > 0) ? (domestic / cycle) + 1 : (domestic / cycle);
foreignCycle = (foreign % cycle > 0) ? (foreign / cycle) + 1 : (foreign / cycle);
foreach (string type in DTtype)
{
Thread.Sleep(1);
int MaxCount = 0; //最大需跑迴圈數
int CurCntIdx = 0; //目前執行到第幾次迴圈
if (type == "D")
{
MaxCount = domesticCycle;
}
else
{
MaxCount = foreignCycle;
}
for (int i = 0; i < MaxCount; i++)
{
ParameterizedThreadStart PTS = new ParameterizedThreadStart(Process1); //(new Class1().Process1
Thread THD = new Thread(PTS);
int StartIndex = (i * 10) + 1; // >= RowNumber 起點
int EndIndex = (i * 10) + cycle; // <= RowNumber 終點
CurCntIdx = i;
Hashtable HtParams = new Hashtable();
HtParams.Add("TYPE", type);
HtParams.Add("STARTINDEX", StartIndex); //start
HtParams.Add("ENDINDEX", EndIndex); //end
HtParams.Add("CURINDEX", i); //current index
HtParams.Add("MAXINDEX", MaxCount); //max index
THD.Start(HtParams);
}
}
}
//假設在另一支Class
public void Process1(object Params)
{
Hashtable HtParams = (Hashtable)Params;
int SrartIndex = Convert.ToInt32(HtParams["STARTINDEX"]);
int EndIndex = Convert.ToInt32(HtParams["ENDINDEX"]);
string TYPE = Convert.ToString(HtParams["TYPE"]); //domestic foreign
int CurIndex = Convert.ToInt32(HtParams["CURINDEX"]);
int MaxIndex = Convert.ToInt32(HtParams["MAXINDEX"]);
int result = 0;
if(TYPE == "D")
{
DataSet dsDomestic = new DataSet();
//找出區間的10筆 (>= SrartIndex AND <= EndIndex) ,作運算...
for (int i = 0; i < 10000000; i++)
{
result += i;
}
Thread.Sleep(1000);
}
else
{
DataSet dsForeign = new DataSet();
//找出區間的10筆 (>= SrartIndex AND <= EndIndex) ,作運算...
for (int i = 0; i < 50000000; i++)
{
result += i;
}
Thread.Sleep(1000);
}
if (CurIndex == (MaxIndex - 1))
{
MessageBox.Show(TYPE + " 全部執行完成");
}
//TEST
//MessageBox.Show( TYPE + " RowNumber() : " + " >=" + SrartIndex.ToString() + " <=" + EndIndex.ToString() + " rueult: " + result.ToString());
}
}
{
List<string> DTtype = new List<string>()
{
"D","F"
};
public MultiThread()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataSet dsDomestic = new DataSet(); //56 ROWS
DataSet dsForeign = new DataSet(); //21 ROWS
int domestic = 56; //假設要執行的資料庫的 DataSet 筆數, 要先排序再做 RowNumber()
int foreign = 21; //假設要執行的資料庫的 DataSet 筆數, 要先排序再做 RowNumber()
int cycle = 10; //假設每次每一執行緒,執行10筆, 若跑不動,則減少 9 ,8 , 7...
int domesticCycle = 0, foreignCycle = 0; //計算要執行的次數
domesticCycle = (domestic % cycle > 0) ? (domestic / cycle) + 1 : (domestic / cycle);
foreignCycle = (foreign % cycle > 0) ? (foreign / cycle) + 1 : (foreign / cycle);
foreach (string type in DTtype)
{
Thread.Sleep(1);
int MaxCount = 0; //最大需跑迴圈數
int CurCntIdx = 0; //目前執行到第幾次迴圈
if (type == "D")
{
MaxCount = domesticCycle;
}
else
{
MaxCount = foreignCycle;
}
for (int i = 0; i < MaxCount; i++)
{
ParameterizedThreadStart PTS = new ParameterizedThreadStart(Process1); //(new Class1().Process1
Thread THD = new Thread(PTS);
int StartIndex = (i * 10) + 1; // >= RowNumber 起點
int EndIndex = (i * 10) + cycle; // <= RowNumber 終點
CurCntIdx = i;
Hashtable HtParams = new Hashtable();
HtParams.Add("TYPE", type);
HtParams.Add("STARTINDEX", StartIndex); //start
HtParams.Add("ENDINDEX", EndIndex); //end
HtParams.Add("CURINDEX", i); //current index
HtParams.Add("MAXINDEX", MaxCount); //max index
THD.Start(HtParams);
}
}
}
//假設在另一支Class
public void Process1(object Params)
{
Hashtable HtParams = (Hashtable)Params;
int SrartIndex = Convert.ToInt32(HtParams["STARTINDEX"]);
int EndIndex = Convert.ToInt32(HtParams["ENDINDEX"]);
string TYPE = Convert.ToString(HtParams["TYPE"]); //domestic foreign
int CurIndex = Convert.ToInt32(HtParams["CURINDEX"]);
int MaxIndex = Convert.ToInt32(HtParams["MAXINDEX"]);
int result = 0;
if(TYPE == "D")
{
DataSet dsDomestic = new DataSet();
//找出區間的10筆 (>= SrartIndex AND <= EndIndex) ,作運算...
for (int i = 0; i < 10000000; i++)
{
result += i;
}
Thread.Sleep(1000);
}
else
{
DataSet dsForeign = new DataSet();
//找出區間的10筆 (>= SrartIndex AND <= EndIndex) ,作運算...
for (int i = 0; i < 50000000; i++)
{
result += i;
}
Thread.Sleep(1000);
}
if (CurIndex == (MaxIndex - 1))
{
MessageBox.Show(TYPE + " 全部執行完成");
}
//TEST
//MessageBox.Show( TYPE + " RowNumber() : " + " >=" + SrartIndex.ToString() + " <=" + EndIndex.ToString() + " rueult: " + result.ToString());
}
}