Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
http://blog.csdn.net/config_man/article/details/25578767
- #region 调用timer控件实时查询开关机时间
- private void timer1_Tick(object sender, EventArgs e)
- {
- string sql = "SELECT startTime,endTime,AMTusername,AMTpassword,AMTip FROM AmtTiming at, AmtComputer ac WHERE at.cid = ac.id";
- List<TimingBean> list = new Database().getRS(sql);
- if (list != null && list.Count > 0)
- {
- foreach (TimingBean tb in list)
- {
- string startTime = tb.StartTime;
- string endTime = tb.EndTime;
- string AMTusername = tb.AMTUsername;
- string AMTpassword = tb.AMTPassword;
- string AMTip = tb.AMTIp;
- string now = DateTime.Now.ToShortTimeString();
- if (startTime == now)
- {
- Action<string, string, string, bool> action = new Action<string, string, string, bool>(StartOrShutDown);
- action.BeginInvoke(AMTusername, AMTpassword, AMTip, true, null, null);
- }
- else if (endTime == now)
- {
- Action<string, string, string, bool> action = new Action<string, string, string, bool>(StartOrShutDown);
- action.BeginInvoke(AMTusername, AMTpassword, AMTip, false, null, null);
- }
- }
- }
- }
- private void StartOrShutDown(string user, string pass, string ip, bool isStart)
- {
- AmtRemoteAControl amtControl = new AmtRemoteAControl();
- if (isStart)
- {
- //如果开机不成功,则让其再执行一次
- try
- {
- amtControl.SendPowerOnCmd(ip, user, pass);
- }
- catch
- {
- try
- {
- amtControl.SendPowerOnCmd(ip, user, pass);
- }
- catch (Exception e)
- {
- MessageBox.Show("终端设备:" + ip + "自动开机失败。异常信息:" + e.Message);
- }
- }
- }
- else
- {
- //如果关机不成功,则让其再执行一次
- try
- {
- amtControl.SendPowerOffCmd(ip, user, pass);
- }
- catch
- {
- try
- {
- amtControl.SendPowerOffCmd(ip, user, pass);
- }
- catch (Exception e)
- {
- MessageBox.Show("终端设备:" + ip + "自动关机失败。异常信息:" + e.Message);
- }
- }
- }
- }
- #endregion
- #region 开关机、重启 "查询按钮"
- bool has = false;
- private void button1_Click(object sender, EventArgs e)
- {
- //获得省份索引和某个市的文本
- int index = this.comboBox1.SelectedIndex;
- if (0 == index)
- {
- MessageBox.Show("请选择区域!"); return;
- }
- else
- {
- #region 获取选择的区域
- this.buttonStart.Enabled = false;
- this.buttonShutdown.Enabled = false;
- this.buttonReStart.Enabled = false;
- string place = this.comboBox1.Text; //省
- int city_index = this.comboBox2.SelectedIndex;//市
- string county = this.comboBox3.Text; //区县
- //如果城市有选择
- if (city_index != 0)
- {
- place = place + this.comboBox2.Text;
- }
- //如果区县有选择
- if ((null != county) && (!((string.Empty).Equals(county))) && (!"--请选择--".Equals(county)))
- {
- place = place + county;
- }
- #endregion
- try
- {
- #region 将查到的设备信息绑定到数据表格
- //将查到的设备信息绑定到数据表格
- //string sql = "SELECT '' as '选择',cp.en '设备编号',cp.ip '设备IP',cp.place '设备地址', cp.AMTusername '用户名',cp.AMTpassword '密码',cp.AMTip 'IP',cp.id '主键',cp.status '状态' FROM AmtComputer cp WHERE cp.place like '%" + place + "%'";
- string sql = "SELECT cp.en '设备编号',cp.ip '设备IP',cp.place '设备地址', cp.AMTusername '用户名',cp.AMTpassword '密码',cp.AMTip 'IP',cp.id '主键',cp.status '状态' FROM AmtComputer cp WHERE cp.place like '%" + place + "%'";
- Database db = new Database();
- DataSet ds = db.getDS(new DataSet(), sql);
- DataTable table = ds.Tables["data"];
- this.bindingSource1.DataSource = table;
- this.dataGridView1.DataSource = this.bindingSource1;
- if(!has)
- {
- //添加复选框
- DataGridViewCheckBoxColumn box = new DataGridViewCheckBoxColumn();
- box.HeaderText = "选择";
- box.Name = "选择";
- this.dataGridView1.Columns.Insert(0, box);
- has = true;
- }
- //设置部分列为不可见状态
- this.dataGridView1.Columns["用户名"].Visible = false;//AMT用户名
- this.dataGridView1.Columns["密码"].Visible = false;//AMT密码
- this.dataGridView1.Columns["IP"].Visible = false;//AMT设备ip
- this.dataGridView1.Columns["主键"].Visible = false;//主键
- this.dataGridView1.Columns["选择"].Width = 60;//复选框
- this.dataGridView1.Columns["设备编号"].Width = 100;//设备编号
- this.dataGridView1.Columns["设备IP"].Width = 140;//设备IP
- this.dataGridView1.Columns["设备地址"].Width = 160;//设备地址
- this.dataGridView1.Columns["状态"].Width = 180;//状态
- #endregion
- //this.labelState.Text = "正在获取设备状态,请稍后...";
- //this.Refresh();
- int count = table.Rows.Count;
- for (int i = 0; i < count; i++)
- {
- string username = table.Rows[i]["用户名"].ToString(); //amt用户名
- string password = table.Rows[i]["密码"].ToString(); //amt密码
- string host = table.Rows[i]["IP"].ToString(); //amtIP地址
- this.dataGridView1.Rows[i].Cells["状态"].Value = "正在获取终端状态...";
- this.dataGridView1.Rows[i].Cells["状态"].Style.ForeColor = Color.Red;
- this.dataGridView1.Rows[i].Cells["状态"].Style.Font = new Font("宋体", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
- Func<int, string, string, string, string> func = new Func<int, string, string, string, string>(getPowerState);
- func.BeginInvoke(i, username, password, host,
- (result) =>
- {
- string state = func.EndInvoke(result);
- this.BeginInvoke(new Action<string>(setStateValue), state);
- }, null);
- }
- }
- catch (Exception ee) { MessageBox.Show(ee.Message); }
- }
- }
- private void setStateValue(string state)
- {
- string[] array = state.Split(',');
- this.dataGridView1.Rows[Convert.ToInt16(array[0])].Cells["状态"].Value = array[1];
- this.buttonStart.Enabled = true;
- this.buttonShutdown.Enabled = true;
- this.buttonReStart.Enabled = true;
- }
- #endregion
- #region 获取amt设备的当前电源状态
- private string getPowerState(int index,string username, string password, string host)
- {
- ConnectionInfo info = new ConnectionInfo(host, username, password, false,
- string.Empty, ConnectionInfo.AuthMethod.Digest,
- null, null);
- DotNetWSManClient wsman = new DotNetWSManClient(info);
- RemoteControlApi api = new RemoteControlApi(wsman);
- try
- {
- CIM_AssociatedPowerManagementService service = api.GetCurrentPowerState(true);
- ushort state = service.PowerState;
- if (state == 2)
- {
- return index + ",开机";
- }
- else if (state == 8)
- {
- return index + ",关机";
- }
- }
- catch
- {
- try
- {
- CIM_AssociatedPowerManagementService service = api.GetCurrentPowerState(false);
- ushort state = service.PowerState;
- if (state == 2)
- {
- return index + ",开机";
- }
- else if (state == 8)
- {
- return index + ",关机";
- }
- }
- catch (Exception e2)
- {
- return index + "," + e2.Message;
- }
- }
- return index + ",未知";
- }
- #endregion
Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)的更多相关文章
- winform 开发中 把耗时操作 封装起来 异步执行(.net 4.0)
.先定义一个 BackgroundTask.cs 代码如下: public class BackgroundTask { private static WaitDialogForm LoadingDl ...
- 事件异步(EAP)使用事件异步处理一些耗时操作
比如需要下载一些比较大的文件,如果使用会UI卡顿,使用异步可以节省一些时间 下面是一些例子: using System; using System.Collections.Generic; using ...
- Winform 界面执行耗时操作--UI卡顿假死问题
UI卡顿假死问题 误区1:使用不同的线程操作UI控件和耗时操作(即,跨线程操作UI控件CheckForIllegalCrossThreadCalls = false;), 注意:此处只是为了记录... ...
- 异步委托(APM)使用Func异步操作,处理耗时操作
使用委托进行异步操作,处理一些耗时操作,防止主线程阻塞 使用例子: using System; using System.Collections.Generic; using System.Linq; ...
- C# winform解决解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题
public frmMain() { InitializeComponent(); //解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题 int x = Convert.ToInt32(( ...
- 谈.Net委托与线程——解决窗体假死
转自:http://www.cnblogs.com/smartls/archive/2011/04/08/2008981.html#2457370 引言 在之前的<创建无阻塞的异步调用> ...
- C# 解决窗体假死的状态
异步调用是CLR为开发者提供的一种重要的编程手段,它也是构建高性能.可伸缩应用程序的关键.在多核CPU越来越普及的今天,异步编程允许使用非常少的线程执行很多操作.我们通常使用异步完成许多计算型.IO型 ...
- ASP.NET服务器端执行耗时操作的工作记录
公司之前有这样一个业务需求: 一名同事做出文件a0和b0,然后将a0加密为a1.b0加密为b1:再将文件a0.a1.b0和b1上传至服务器M:同时要将服务器N上的数据表添加一条记录,该记录的ID就是前 ...
- (二十三)c#Winform自定义控件-等待窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- html5-新布局元素header,footer
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- Python 7 -- 文件存储数据
上一节总结了一个基本web应用的代码,这一节主要讲用户访问的数据记录在log文件中,并显示在页面上. 这节步骤: 按以下目录建好相应的文件夹及内容 webapp|----vsearch4web.py ...
- XML系列之--对电文格式XML的简单操作(三)
前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...
- jdk和jre区别
- Spring源码阅读(二)
我们先看AbstractBeanFactory.getBean方法,这个方法通过bean名称类型等信息获取类实例,如果实例不存在则生产并缓存. //-------------------------- ...
- 转:【专题九】实现类似QQ的即时通信程序
引言: 前面专题中介绍了UDP.TCP和P2P编程,并且通过一些小的示例来让大家更好的理解它们的工作原理以及怎样.Net类库去实现它们的.为了让大家更好的理解我们平常中常见的软件QQ的工作原理,所以在 ...
- linux 安装python3 date更新
http://linux.51yip.com/ ntpdate -u ntp.aliyun.com 更新时间 centos 默认是有 python的,是2.7.5的 重启网络的命令 -- sys ...
- Java开发学习教程之对象的创建与使用
java面向对象中的对象创建与使用.类是对象的抽象,为对象定义了属性和行为,但类本身既不带任何数据,也不存在于内存空间中.而对象是类的一个具体存在,既拥有独立的内存空间,也存在独特的属性和行为,属性还 ...
- Linux下解析域名命令-dig 命令使用详解
Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息.dig 命令主要用来从 DNS 域名服务器查询主机地址信息.dig的全称是 (dom ...
- node 读取多个文件、合并多个文件、读写多个文件
一.读取文件 1.找文件中匹配的内容 let fs = require('fs') let content = fs.readFileSync('/Users/**/desktop/Test.txt' ...