微软提供了Invoke的方法,其作用就是让子线程告诉窗体线程来完成相应的控件操作。
private Thread opThread=null;
private delegate void MyInvoker(string str);
private MyInvoker myInv = null;
private MyInvoker barInv = null;
private delegate bool IncreaseHandle( int nValue );
//查询 、插入的 循环次数
private int OPER_CIRCLE_TIMES=7;
/***查找插入***/#region/***查找插入***/
/**//// <summary>
/// 从三个表中提取数据,暂存到DataSet中,后加载到listview显示,并查找A及B,
/// 然后,将所有数据插入到最终表中。
/// </summary>
/// <param name="operInt"></param>
/// <returns></returns>
private int InsertNull(int operInt)
{
try
{
//查找记录
GetData dataInfo=new GetData();
dataInfo.ConnString=this.p_operTablePath;
DataSet myDs=dataInfo.GetTreeDataSet(operInt);
//绑定到listview
DataBind(myDs);
//向StatckInfo表,插入数据
if(this.p_Result==1)
{
InsertDataFromDataSet2(dataInfo,myDs);
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return this.p_Result;
}
#endregion
/***逐行插入***/#region/***逐行插入***/
/**//// <summary>
/// listview 循环插入
/// this.lv_Data.Items[i].Tag;
/// </summary>
/// <param name="dataInfo">GetData dataInfo</param>
/// <param name="ds">DataSet ds</param>
private void InsertDataFromDataSet2(GetData dataInfo,DataSet ds)
{
if (ds==null)
{
return;
}
try
{
//总行数
int total =ds.Tables[0].Rows.Count;
//进度条初始化
// this.prgBar.Maximum=total;
// prgBar.Value=0;
Bar(total.ToString());
for(int i=0;i<total;i++)
{
StatckInfo editstatckInfo=(StatckInfo)this.lv_Data.Items[i].Tag;
editstatckInfo.ConnString=this.p_operTablePath;
bool flag=editstatckInfo.OperateLocalDInfomation(editstatckInfo,0);
//Label操作过程提示
// this.labInfo.Text="正在插入的证券号是:"+editstatckInfo.IdReceipt.ToString();
// this.labInfo.Refresh();
WriteInfo("正在插入的**号是:"+editstatckInfo.IdReceipt.ToString());
//进度条滚动
//this.prgBar.Value=prgBar.Value+1;
Bar("1");
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
/***DataBind***/#region/***DataBind***/
/**//// <summary>
/// 从DataTable中放数据到ListView中
/// </summary>
/// <param name="ds">DataSet ds</param>
public void DataBind(DataSet ds)
{
if (ds==null)
{
this.lv_Data.Items.Clear();
return;
}
try
{
DataTable Dtab =ds.Tables[0];
ArrayList itemArray=new ArrayList();
//进度条初始化
// this.prgBar.Maximum=Dtab.Rows.Count;
// prgBar.Value=0;
//进度条初始化
int total=Dtab.Rows.Count;
Bar(total.ToString());
foreach(DataRow Row in Dtab.Rows)
{
object IDQuestion=Row[0];
object IDType=Row[1];
object CustomerName= Row[2];
object CustomerID=Row[3];
object RMoney= Row[4];
object receiptID= Row[5];
object noShanZheng= Row[6];
object snChanQuan= Row[7];
object address= Row[8];
object Phone=Row[9];
int sn=System.Convert.ToInt32(IDQuestion);
int typeid=System.Convert.ToInt32(IDType);
//构造S**&^&Info类
StatckInfo tempStack=new StatckInfo(sn,typeid,CustomerName.ToString(),
CustomerID.ToString(),RMoney.ToString(),receiptID.ToString(),noShanZheng.ToString(),
snChanQuan.ToString(),address.ToString(),Phone.ToString());
//查找AA等
GetData dataInfo=new GetData();
dataInfo.ConnString=this.p_operTablePath;
dataInfo.CombinStatckFileds(tempStack);
ListViewItem lvi=this.GenLvItem(tempStack);
itemArray.Add(lvi);
//Label操作过程提示
// this.labInfo.Text="查询" +typeid.ToString()+ "表的**号是:"+receiptID.ToString();
// this.labInfo.Refresh();
WriteInfo("查询" +typeid.ToString()+ "表的**号是:"+receiptID.ToString());
//进度条滚动
// this.prgBar.Value=prgBar.Value+1;
Bar("1");
}
this.FillListView(itemArray,this.lv_Data);
Result=1;
}
catch (Exception err)
{
Result=-1;
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
/***多线程处理*/#region/***多线程处理*/
//子线程入口函数:
/**//// <summary>
/// Thread function interface
/// </summary>
private void ThreadFun()
{
try
{
ParameteTrans();
for( int i =1; i < OPER_CIRCLE_TIMES; i++ )
{
Thread.Sleep(20);
InsertNull(i);
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
MessageBox.Show(this,"数据插入完毕!","插入数据",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.prgBar.Visible=false;
this.labInfo.Visible=false;
this.cmdDeletSame.Enabled=true;
}
/**//// <summary>
/// 改变数值
/// </summary>
private void ParameteTrans()
{
// Init increase event
myInv = new MyInvoker(DisplayInfo);
barInv = new MyInvoker(changebar);
}
private void WriteInfo(string str)
{
this.Invoke(this.myInv,
new object[] { str });
}
private void Bar(string i)
{
this.Invoke(this.barInv, new object[] { i });
}
private void DisplayInfo(string str)
{
labInfo.Text = str;
}
private void changebar(string i)
{
try
{
int myint=int.Parse(i);
if (myint > 1)
{
prgBar.Maximum = myint;
prgBar.Value = 0;
}
else
prgBar.Value += myint;
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 创建子线程
/// </summary>
public void InitThis()
{
try
{
opThread=new Thread( new ThreadStart(ThreadFun));
opThread.IsBackground=true;
opThread.Start();
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
private delegate void MyInvoker(string str);
private MyInvoker myInv = null;
private MyInvoker barInv = null;
private delegate bool IncreaseHandle( int nValue );
//查询 、插入的 循环次数
private int OPER_CIRCLE_TIMES=7;
/***查找插入***/#region/***查找插入***/
/**//// <summary>
/// 从三个表中提取数据,暂存到DataSet中,后加载到listview显示,并查找A及B,
/// 然后,将所有数据插入到最终表中。
/// </summary>
/// <param name="operInt"></param>
/// <returns></returns>
private int InsertNull(int operInt)
{
try
{
//查找记录
GetData dataInfo=new GetData();
dataInfo.ConnString=this.p_operTablePath;
DataSet myDs=dataInfo.GetTreeDataSet(operInt);
//绑定到listview
DataBind(myDs);
//向StatckInfo表,插入数据
if(this.p_Result==1)
{
InsertDataFromDataSet2(dataInfo,myDs);
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return this.p_Result;
}
#endregion
/***逐行插入***/#region/***逐行插入***/
/**//// <summary>
/// listview 循环插入
/// this.lv_Data.Items[i].Tag;
/// </summary>
/// <param name="dataInfo">GetData dataInfo</param>
/// <param name="ds">DataSet ds</param>
private void InsertDataFromDataSet2(GetData dataInfo,DataSet ds)
{
if (ds==null)
{
return;
}
try
{
//总行数
int total =ds.Tables[0].Rows.Count;
//进度条初始化
// this.prgBar.Maximum=total;
// prgBar.Value=0;
Bar(total.ToString());
for(int i=0;i<total;i++)
{
StatckInfo editstatckInfo=(StatckInfo)this.lv_Data.Items[i].Tag;
editstatckInfo.ConnString=this.p_operTablePath;
bool flag=editstatckInfo.OperateLocalDInfomation(editstatckInfo,0);
//Label操作过程提示
// this.labInfo.Text="正在插入的证券号是:"+editstatckInfo.IdReceipt.ToString();
// this.labInfo.Refresh();
WriteInfo("正在插入的**号是:"+editstatckInfo.IdReceipt.ToString());
//进度条滚动
//this.prgBar.Value=prgBar.Value+1;
Bar("1");
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
/***DataBind***/#region/***DataBind***/
/**//// <summary>
/// 从DataTable中放数据到ListView中
/// </summary>
/// <param name="ds">DataSet ds</param>
public void DataBind(DataSet ds)
{
if (ds==null)
{
this.lv_Data.Items.Clear();
return;
}
try
{
DataTable Dtab =ds.Tables[0];
ArrayList itemArray=new ArrayList();
//进度条初始化
// this.prgBar.Maximum=Dtab.Rows.Count;
// prgBar.Value=0;
//进度条初始化
int total=Dtab.Rows.Count;
Bar(total.ToString());
foreach(DataRow Row in Dtab.Rows)
{
object IDQuestion=Row[0];
object IDType=Row[1];
object CustomerName= Row[2];
object CustomerID=Row[3];
object RMoney= Row[4];
object receiptID= Row[5];
object noShanZheng= Row[6];
object snChanQuan= Row[7];
object address= Row[8];
object Phone=Row[9];
int sn=System.Convert.ToInt32(IDQuestion);
int typeid=System.Convert.ToInt32(IDType);
//构造S**&^&Info类
StatckInfo tempStack=new StatckInfo(sn,typeid,CustomerName.ToString(),
CustomerID.ToString(),RMoney.ToString(),receiptID.ToString(),noShanZheng.ToString(),
snChanQuan.ToString(),address.ToString(),Phone.ToString());
//查找AA等
GetData dataInfo=new GetData();
dataInfo.ConnString=this.p_operTablePath;
dataInfo.CombinStatckFileds(tempStack);
ListViewItem lvi=this.GenLvItem(tempStack);
itemArray.Add(lvi);
//Label操作过程提示
// this.labInfo.Text="查询" +typeid.ToString()+ "表的**号是:"+receiptID.ToString();
// this.labInfo.Refresh();
WriteInfo("查询" +typeid.ToString()+ "表的**号是:"+receiptID.ToString());
//进度条滚动
// this.prgBar.Value=prgBar.Value+1;
Bar("1");
}
this.FillListView(itemArray,this.lv_Data);
Result=1;
}
catch (Exception err)
{
Result=-1;
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
/***多线程处理*/#region/***多线程处理*/
//子线程入口函数:
/**//// <summary>
/// Thread function interface
/// </summary>
private void ThreadFun()
{
try
{
ParameteTrans();
for( int i =1; i < OPER_CIRCLE_TIMES; i++ )
{
Thread.Sleep(20);
InsertNull(i);
}
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
MessageBox.Show(this,"数据插入完毕!","插入数据",MessageBoxButtons.OK,MessageBoxIcon.Information);
this.prgBar.Visible=false;
this.labInfo.Visible=false;
this.cmdDeletSame.Enabled=true;
}
/**//// <summary>
/// 改变数值
/// </summary>
private void ParameteTrans()
{
// Init increase event
myInv = new MyInvoker(DisplayInfo);
barInv = new MyInvoker(changebar);
}
private void WriteInfo(string str)
{
this.Invoke(this.myInv,
new object[] { str });
}
private void Bar(string i)
{
this.Invoke(this.barInv, new object[] { i });
}
private void DisplayInfo(string str)
{
labInfo.Text = str;
}
private void changebar(string i)
{
try
{
int myint=int.Parse(i);
if (myint > 1)
{
prgBar.Maximum = myint;
prgBar.Value = 0;
}
else
prgBar.Value += myint;
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
/**//// <summary>
/// 创建子线程
/// </summary>
public void InitThis()
{
try
{
opThread=new Thread( new ThreadStart(ThreadFun));
opThread.IsBackground=true;
opThread.Start();
}
catch (Exception err)
{
MessageBox.Show(this,err.Message,"",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion