/// <summary>
/// 线程控制模块
/// </summary>
private ManualResetEvent manualResetEvent = new ManualResetEvent(false);
/// <summary>
/// 配合使用
/// </summary>
private AutoResetEvent autoResetEvent = new AutoResetEvent(true);
private AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null); /// <summary>
/// 存储状态
/// </summary>
private bool suspend; /// <summary>
/// 委托操作模块
/// </summary>
/// <param name="str"></param>
private delegate void InvokeDelegate(string str);
private delegate void InvokeMessage(int result);
private delegate string[] InvokeMessageResult(int result);
private delegate void InvokeListView(string oneValue, string twoValue, string threeValue, string four,string five);
private delegate void InvokeMessageUI(string msg, string value);
private delegate void InvokeMessageViewUI(string[] value);
private delegate void InvokeExecute(); /// <summary>
///
/// </summary>
private bool formClosed = false; private void btnDowload_Click(object sender, EventArgs e)
{
//启动下载任务
if (this.btnDowload.Tag == null)
{
ThreadPool.QueueUserWorkItem(delegate
{
ExecuteDownload(this.txtPath.Text);
});
}
else if (this.btnDowload.Tag.ToString() == "Run")
{
this.suspend = true;
manualResetEvent.Reset();
//autoResetEvent.Reset();//另一种方式 //更改执行按钮状态
EvenInvokeButtionProcess("继续下载", "Reset");
}
else if (this.btnDowload.Tag.ToString() == "Reset")
{
//
this.suspend = false;
manualResetEvent.Set();
//autoResetEvent.Set();//另一种方式 //更改执行按钮状态
EvenInvokeButtionProcess("暂停下载", "Run");
}
/** ----------------------------------------
* ThreadPool.QueueUserWorkItem(delegate
* {
* ExecuteDownload(this.txtPath.Text);
* });
* -----------------挂起下载任务------------
* this.suspend = true;
* manualResetEvent.Reset();
* //autoResetEvent.Reset();//另一种方式
* -----------------唤醒下载任务-------------
* this.suspend = false;
* manualResetEvent.Set();
* //autoResetEvent.Set();//另一种方式
***/
} /// <summary>
/// 下载档案文件
/// </summary>
public void ExecuteDownload(string savePath)
{
//EvenInvokeButtionState("btnDowload", "false");
//更改执行按钮状态
EvenInvokeButtionProcess("暂停下载", "Run");
//更改控件展示状态
EvenInvokeButtionState("btnGet", "false");
//初始化进度条
EventInvokeProgresBarInit();
//初始化进度条最大值
int count = this.listView1.Items.Count;
EventProgresMaximum(count);
//this.listView1.Items 的下标从0开始,所以取值时,下标为 Count - 1
for (int i = this.listView1.Items.Count; i > ; i--)
{
int rowIndex = ;
//1.取得一条数据,
if (i == )
{
rowIndex = i;
}
else
{
rowIndex = i - ;
}
/**
* this.listView1.Items 的下标从0开始,所以取值时,下标为 Count - 1,因为 Count 计数器,是从 1开始记录,所以相差1
* */
EventListViewSelected(rowIndex);
//获取列表
string[] ls = EventListViewGet(rowIndex);
//更新操作状态
EventInvokeListBoxText("---------------------------------------------------------", "");
EventInvokeListBoxText("开始下载:", ls[].ToString() + "档案文件");
//更新比例
EventProgresTipText(count.ToString(), "/" + (rowIndex));
//执行下载操作
ExecuteDownload(ls[].ToString(), ls[].ToString(), savePath);
//将比对完成之后的数据移除
EventListViewRemoveAt(rowIndex);
//更新进度条状态
EventInvokeProgresBar();
//---------------------------------------------------------------------------
if (formClosed)
{
return;
}
//监听任务状态
if (suspend)
{
//更改执行按钮状态
EvenInvokeButtionProcess("继续下载", "Reset");
//挂起任务
this.manualResetEvent.WaitOne();
}
/** -------------------------另一种方式-------------------------------------
* if (suspend)
* {
* this.autoResetEvent.WaitOne();
* }
* asyncOperation.Post(delegate { EventMessageBox(str); }, str);
**/
}
//更新进度条状态
EventInvokeProgresBar(-); //-100 为状态位
//设置按钮状态
EvenInvokeButtionState("btnGet", "true");
//EvenInvokeButtionState("btnDowload", "true");
//更改执行按钮状态
EvenInvokeButtionProcess("文件下载", null);
//数据加载完毕,设置提示信息
EventInvokeListBoxText("---------------------------------------------------------","");
EventInvokeListBoxText("执行状态:", "档案文件已经下载完毕,等待相关任务执行中.........");
} /// <summary>
/// 进度提示委托
/// </summary>
/// <param name="mes"></param>
/// <param name="value"></param>
public void EventInvokeListBoxText(string msg, string value)
{
this.Invoke(new InvokeMessageUI(InvokeListBoxText), new object[] { msg, value });
} /// <summary>
/// 进度提示条改变
/// </summary>
/// <param name="mes"></param>
/// <param name="value"></param>
public void InvokeListBoxText(string msg, string value)
{
if (this.listBox1.Items.Count == )
{
this.listBox1.Items.Insert(, msg + value);
}
else
{
this.listBox1.Items.Insert(this.listBox1.Items.Count -, msg + value);
}
//this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);
} /// <summary>
///
/// </summary>
/// <param name="msg"></param>
/// <param name="value"></param>
private void EvenInvokeButtionProcess(string msg, string value)
{
this.Invoke(new InvokeMessageUI(InvokeButtionProcessUI), new object[] { msg, value });
} private void InvokeButtionProcessUI(string msg, string value)
{
this.btnDowload.Tag = value;
this.btnDowload.Text = msg;
}

最新文章

  1. 2016 windows安装phing:安装成功
  2. HTML网页内容转换成字符串(删除从指定字符串到指定字符串)
  3. 《奥威Power-BI智能分析报告制作方法 》精彩回顾
  4. java学习规划
  5. ZOJ 3879 Capture the Flag
  6. sql 中条件in参数问题
  7. A Brief Introduction to Multiset[STL]
  8. 工作流--JBPM核心服务及表结构
  9. 自定义Dialog,从下面弹出
  10. 网友RHCE认证考试经历
  11. JAVA定时任务实现的几种方式
  12. 【技巧】-NO.123.数据处理技巧
  13. backbond Model实现
  14. day 67 orm初识 {code_first/db_first}
  15. HTML5仿微信公众号界面
  16. c++设计一个无法被继承的类
  17. Linux内核 GPIO操作部分API
  18. 关于 RabbitMQ 的 Dead-Letters-Queue “死信队列”
  19. MySQL 设置密码有效时间
  20. python 直方图hist

热门文章

  1. php 内置函数JSON处理
  2. 页面可视化编辑ckeditor(web基础学习笔记十五)
  3. 算法笔记_195:历届试题 错误票据(Java)
  4. Loadrunner错误-26601、-27492、-27727处理方法
  5. JDK核心JAVA源代码解析(1) - Object
  6. oracle 建表时显示ORA-00984: 列在此处不允许
  7. Design Pattern Bridge 桥设计模式
  8. php 5.3新特性
  9. MyEclipse下的Debug调试技巧汇总
  10. Matlab 调用Oracle数据库