委托+线程 使用例子
来源:互联网 发布:javascript 清空table 编辑:程序博客网 时间:2024/06/01 09:11
/// <summary>
/// 委托
/// </summary>
private delegate void ThreadDelegate();//用于设置进度条当前值 的委托
string filter=string.Empty;//选择策略
Microsoft.Office.Interop.Excel.Application excel = null;//Excel组件
int rowIndex = 1;//行
int colIndex = 0;//列
Microsoft.Office.Interop.Excel._Workbook xWB = null;
bool flage=false;//控制线程
DataTable table = null;//数据集
Thread _thread =null;
private void ExplortData()
{
string name = DoSave();
if(name!=string.Empty)
{
DataSet explords = new DataSet ();
explords = iQueryFanLiCuiShou.GetFanLiXieYiOfIsGroupMX(filter,ExplorFilter());
if(explords!=null)
{
flage = true;
if(explords.Tables[0].Columns.Contains("IsJTZCType"))
explords.Tables[0].Columns["IsJTZCType"].ColumnName="政策类型";
if(explords.Tables[0].Columns.Contains("DepartName"))
explords.Tables[0].Columns["DepartName"].ColumnName="需方单位";
this.progressBar1.Maximum=explords.Tables[0].Rows.Count;
try
{
excel = new Microsoft.Office.Interop.Excel.Application();
xWB = excel.Application.Workbooks.Add(true);
table = new DataTable ();
table = explords.Tables[0];
this.progressBar1.Maximum=table.Rows.Count+1;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
_thread = new Thread (new ThreadStart(doExport));
_thread.Start();
}
}
}
/// <summary>
/// 在控件句柄线程上执行委托
/// </summary>
private void ThreadProcessBar()
{
progressBar1.Invoke(new ThreadDelegate(SetPBarValue));
}
/// <summary>
/// 控制进度条进度
/// </summary>
private void SetPBarValue()
{
if (flage)
progressBar1.Value = rowIndex;
else
progressBar1.Value = 0;
}
/// <summary>
/// 执行导出
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>
private void doExport()
{
try
{
while (flage)
{
foreach(DataRow row in table.Rows)
{
ThreadProcessBar();
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] =row[col.ColumnName].ToString();
}
}
if(rowIndex==table.Rows.Count+1)
{
flage=false;
rowIndex=1;
}
}
if(!flage)
{
xWB.SaveCopyAs(saveFileDlg.FileName);
xWB.Close(false, null, null);
excel.Quit();
excel = null;
GC.Collect();//垃圾回收
MessageBox.Show("导出成功!");
ThreadProcessBar();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
System.Environment.CurrentDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
}
}
- 委托+线程 使用例子
- C# 使用委托例子
- c# 使用委托操作线程
- C# 使用委托开始线程
- java线程使用例子
- 使用线程池例子
- 线程池使用例子
- 委托例子
- 委托例子
- 调用委托后,资源占用哪个线程的? (一个例子)
- [C#]线程中处理界面委托的小例子
- C# 使用委托跨线程通讯
- 使用MethodInvoker委托进行跨线程访问
- 关于使用委托的线程备忘录
- 使用委托的异步调用实现线程
- c#中使用委托操作线程
- C# 使用委托跨线程通讯
- 线程锁 线程并发处理 | 异步调用委托的使用
- 29.VC(custom)-DDK、设备管理器相关
- 安装clusterware出错及解决
- Linux内核定时器
- 编译全过程
- 孙鑫VC++讲座笔记Lesson 6 菜单编程
- 委托+线程 使用例子
- 孙鑫VC++讲座笔记 LESSON 9
- Android安全退出多Activity与中止应用程序进程的探索
- sphinx+mysql 在中文搜索中的尝试应用
- MODIS数据免费下载
- Linux下tail命令
- Linux下more命令
- .net序列化
- 【观点】10年软件开发教会我最重要的10件事