Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
来源:互联网 发布:mac五国循环重启 编辑:程序博客网 时间:2024/06/03 13:55
#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
0 0
- Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
- 委托 Action系统无返回值委托 Func有返回委托
- C#.NET:高级编程之系统内置委托(有Func/无返回值Action)
- java基础之IO流--ObjectOutputStream(专门用于操作对象)
- [疯狂Java]集合:专门用于聚集操作的一次性集合——Stream(流)
- UNICODE 和_UNICODE 的区别,前者没有下划 线,专门用于Windows 头文件;后者有一个前缀下划线,专门用于C 运行时头文 件。
- java基础之IO流--DataOutputStream(专门用于操作Java基本数据类型的IO流对象)
- [RunLoop 让返回值在耗时操作完成后返回]
- Renderer (专门用于渲染3D 的接口 Renderer )()
- 专门用于画图的设备描述表类
- 专门用于消息回调窗口的窗口标识
- 专门用于微信公众平台的Javascript API
- *& 连用,用于返回类型
- func 返回一个整型值
- ajax动态添加tr td (并用PHP返回值设置tr样式,用于删除操作
- 关键词DISTINCT 用于返回唯一不同的值
- 获取存储过程的返回值,用于其他变量中
- auto decltype 用于返回值类型后置时的占位
- [IOS]来回加载大量图片内存溢出的问题
- 归并排序的迭代算法
- DWZ使用笔记-1 刷新页面
- JavaScript 的原型对象 Prototype
- 柳条鱼入队
- Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)
- 理解js中的原型链
- [LeetCode14] Longest Common Prefix
- 是东莞市的火锅店发货
- IIS权限设置
- codeforces 429A Xor-tree
- java.lang.IllegalArgumentException: id to load is required for loading
- 建站学习笔记(一)开篇+WWW
- 递归将树转化成json字符串