http://blog.csdn.net/config_man/article/details/25578767

    1. #region 调用timer控件实时查询开关机时间
    2. private void timer1_Tick(object sender, EventArgs e)
    3. {
    4. string sql = "SELECT startTime,endTime,AMTusername,AMTpassword,AMTip FROM AmtTiming at, AmtComputer ac WHERE at.cid = ac.id";
    5. List<TimingBean> list = new Database().getRS(sql);
    6. if (list != null && list.Count > 0)
    7. {
    8. foreach (TimingBean tb in list)
    9. {
    10. string startTime = tb.StartTime;
    11. string endTime = tb.EndTime;
    12. string AMTusername = tb.AMTUsername;
    13. string AMTpassword = tb.AMTPassword;
    14. string AMTip = tb.AMTIp;
    15. string now = DateTime.Now.ToShortTimeString();
    16. if (startTime == now)
    17. {
    18. Action<string, string, string, bool> action = new Action<string, string, string, bool>(StartOrShutDown);
    19. action.BeginInvoke(AMTusername, AMTpassword, AMTip, true, null, null);
    20. }
    21. else if (endTime == now)
    22. {
    23. Action<string, string, string, bool> action = new Action<string, string, string, bool>(StartOrShutDown);
    24. action.BeginInvoke(AMTusername, AMTpassword, AMTip, false, null, null);
    25. }
    26. }
    27. }
    28. }
    29. private void StartOrShutDown(string user, string pass, string ip, bool isStart)
    30. {
    31. AmtRemoteAControl amtControl = new AmtRemoteAControl();
    32. if (isStart)
    33. {
    34. //如果开机不成功,则让其再执行一次
    35. try
    36. {
    37. amtControl.SendPowerOnCmd(ip, user, pass);
    38. }
    39. catch
    40. {
    41. try
    42. {
    43. amtControl.SendPowerOnCmd(ip, user, pass);
    44. }
    45. catch (Exception e)
    46. {
    47. MessageBox.Show("终端设备:" + ip + "自动开机失败。异常信息:" + e.Message);
    48. }
    49. }
    50. }
    51. else
    52. {
    53. //如果关机不成功,则让其再执行一次
    54. try
    55. {
    56. amtControl.SendPowerOffCmd(ip, user, pass);
    57. }
    58. catch
    59. {
    60. try
    61. {
    62. amtControl.SendPowerOffCmd(ip, user, pass);
    63. }
    64. catch (Exception e)
    65. {
    66. MessageBox.Show("终端设备:" + ip + "自动关机失败。异常信息:" + e.Message);
    67. }
    68. }
    69. }
    70. }
    71. #endregion
    72. #region 开关机、重启 "查询按钮"
    73. bool has = false;
    74. private void button1_Click(object sender, EventArgs e)
    75. {
    76. //获得省份索引和某个市的文本
    77. int index = this.comboBox1.SelectedIndex;
    78. if (0 == index)
    79. {
    80. MessageBox.Show("请选择区域!"); return;
    81. }
    82. else
    83. {
    84. #region 获取选择的区域
    85. this.buttonStart.Enabled = false;
    86. this.buttonShutdown.Enabled = false;
    87. this.buttonReStart.Enabled = false;
    88. string place = this.comboBox1.Text;              //省
    89. int city_index = this.comboBox2.SelectedIndex;//市
    90. string county = this.comboBox3.Text;        //区县
    91. //如果城市有选择
    92. if (city_index != 0)
    93. {
    94. place = place + this.comboBox2.Text;
    95. }
    96. //如果区县有选择
    97. if ((null != county) && (!((string.Empty).Equals(county))) && (!"--请选择--".Equals(county)))
    98. {
    99. place = place + county;
    100. }
    101. #endregion
    102. try
    103. {
    104. #region 将查到的设备信息绑定到数据表格
    105. //将查到的设备信息绑定到数据表格
    106. //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 + "%'";
    107. 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 + "%'";
    108. Database db = new Database();
    109. DataSet ds = db.getDS(new DataSet(), sql);
    110. DataTable table = ds.Tables["data"];
    111. this.bindingSource1.DataSource = table;
    112. this.dataGridView1.DataSource = this.bindingSource1;
    113. if(!has)
    114. {
    115. //添加复选框
    116. DataGridViewCheckBoxColumn box = new DataGridViewCheckBoxColumn();
    117. box.HeaderText = "选择";
    118. box.Name = "选择";
    119. this.dataGridView1.Columns.Insert(0, box);
    120. has = true;
    121. }
    122. //设置部分列为不可见状态
    123. this.dataGridView1.Columns["用户名"].Visible = false;//AMT用户名
    124. this.dataGridView1.Columns["密码"].Visible = false;//AMT密码
    125. this.dataGridView1.Columns["IP"].Visible = false;//AMT设备ip
    126. this.dataGridView1.Columns["主键"].Visible = false;//主键
    127. this.dataGridView1.Columns["选择"].Width = 60;//复选框
    128. this.dataGridView1.Columns["设备编号"].Width = 100;//设备编号
    129. this.dataGridView1.Columns["设备IP"].Width = 140;//设备IP
    130. this.dataGridView1.Columns["设备地址"].Width = 160;//设备地址
    131. this.dataGridView1.Columns["状态"].Width = 180;//状态
    132. #endregion
    133. //this.labelState.Text = "正在获取设备状态,请稍后...";
    134. //this.Refresh();
    135. int count = table.Rows.Count;
    136. for (int i = 0; i < count; i++)
    137. {
    138. string username = table.Rows[i]["用户名"].ToString(); //amt用户名
    139. string password = table.Rows[i]["密码"].ToString(); //amt密码
    140. string host = table.Rows[i]["IP"].ToString();     //amtIP地址
    141. this.dataGridView1.Rows[i].Cells["状态"].Value = "正在获取终端状态...";
    142. this.dataGridView1.Rows[i].Cells["状态"].Style.ForeColor = Color.Red;
    143. this.dataGridView1.Rows[i].Cells["状态"].Style.Font = new Font("宋体", 11F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
    144. Func<int, string, string, string, string> func = new Func<int, string, string, string, string>(getPowerState);
    145. func.BeginInvoke(i, username, password, host,
    146. (result) =>
    147. {
    148. string state = func.EndInvoke(result);
    149. this.BeginInvoke(new Action<string>(setStateValue), state);
    150. }, null);
    151. }
    152. }
    153. catch (Exception ee) { MessageBox.Show(ee.Message); }
    154. }
    155. }
    156. private void setStateValue(string state)
    157. {
    158. string[] array = state.Split(',');
    159. this.dataGridView1.Rows[Convert.ToInt16(array[0])].Cells["状态"].Value = array[1];
    160. this.buttonStart.Enabled = true;
    161. this.buttonShutdown.Enabled = true;
    162. this.buttonReStart.Enabled = true;
    163. }
    164. #endregion
    165. #region 获取amt设备的当前电源状态
    166. private string getPowerState(int index,string username, string password, string host)
    167. {
    168. ConnectionInfo info = new ConnectionInfo(host, username, password, false,
    169. string.Empty, ConnectionInfo.AuthMethod.Digest,
    170. null, null);
    171. DotNetWSManClient wsman = new DotNetWSManClient(info);
    172. RemoteControlApi api = new RemoteControlApi(wsman);
    173. try
    174. {
    175. CIM_AssociatedPowerManagementService service = api.GetCurrentPowerState(true);
    176. ushort state = service.PowerState;
    177. if (state == 2)
    178. {
    179. return index + ",开机";
    180. }
    181. else if (state == 8)
    182. {
    183. return index + ",关机";
    184. }
    185. }
    186. catch
    187. {
    188. try
    189. {
    190. CIM_AssociatedPowerManagementService service = api.GetCurrentPowerState(false);
    191. ushort state = service.PowerState;
    192. if (state == 2)
    193. {
    194. return index + ",开机";
    195. }
    196. else if (state == 8)
    197. {
    198. return index + ",关机";
    199. }
    200. }
    201. catch (Exception e2)
    202. {
    203. return index + "," + e2.Message;
    204. }
    205. }
    206. return index + ",未知";
    207. }
    208. #endregion

Winform异步解决窗体耗时操作(Action专门用于无返回值,Func专门用于有返回值)的更多相关文章

  1. winform 开发中 把耗时操作 封装起来 异步执行(.net 4.0)

    .先定义一个 BackgroundTask.cs 代码如下: public class BackgroundTask { private static WaitDialogForm LoadingDl ...

  2. 事件异步(EAP)使用事件异步处理一些耗时操作

    比如需要下载一些比较大的文件,如果使用会UI卡顿,使用异步可以节省一些时间 下面是一些例子: using System; using System.Collections.Generic; using ...

  3. Winform 界面执行耗时操作--UI卡顿假死问题

    UI卡顿假死问题 误区1:使用不同的线程操作UI控件和耗时操作(即,跨线程操作UI控件CheckForIllegalCrossThreadCalls = false;), 注意:此处只是为了记录... ...

  4. 异步委托(APM)使用Func异步操作,处理耗时操作

    使用委托进行异步操作,处理一些耗时操作,防止主线程阻塞 使用例子: using System; using System.Collections.Generic; using System.Linq; ...

  5. C# winform解决解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题

    public frmMain() { InitializeComponent(); //解决窗体第一次设置为最大化后,点击最大化按钮窗体无法居中问题 int x = Convert.ToInt32(( ...

  6. 谈.Net委托与线程——解决窗体假死

    转自:http://www.cnblogs.com/smartls/archive/2011/04/08/2008981.html#2457370   引言 在之前的<创建无阻塞的异步调用> ...

  7. C# 解决窗体假死的状态

    异步调用是CLR为开发者提供的一种重要的编程手段,它也是构建高性能.可伸缩应用程序的关键.在多核CPU越来越普及的今天,异步编程允许使用非常少的线程执行很多操作.我们通常使用异步完成许多计算型.IO型 ...

  8. ASP.NET服务器端执行耗时操作的工作记录

    公司之前有这样一个业务需求: 一名同事做出文件a0和b0,然后将a0加密为a1.b0加密为b1:再将文件a0.a1.b0和b1上传至服务器M:同时要将服务器N上的数据表添加一条记录,该记录的ID就是前 ...

  9. (二十三)c#Winform自定义控件-等待窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. I/O流的概念和流类库的结构

    概念: 程序的输入指的是从输入文件将数据传送给程序,程序的输出指的是从程序将数据传送给输出文件. C++输入输出包含以下三个方面的内容: 1.对系统指定的标准设备的输入和输出.即从键盘输入数据,输出到 ...

  2. html5-新元素新布局模板-完善中

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. sitecore系列教程之Sitecore个性化-体验概况概述

    SITECORE 8:体验概况概述 什么是体验简介? 体验配置文件是Sitecore中的仪表板应用程序,它说明了客户体验和交互的关键区域,例如访问者详细信息,访问,活动,目标,配置文件,自动化等等. ...

  4. MongoDB3.X单机及shading cluster集群的权限管理(基于3.4.5)

    mongodb集群的权限管理分为两部分,一部分是最常用的Role-Based Access Control,也就是用户名密码方式,这种验证方式一般出现在单机系统,或者集群中client端连接Mongo ...

  5. Web3.js API 中文文档

    Web3.js API 中文文档 http://web3.tryblockchain.org/Web3.js-api-refrence.html web3对象提供了所有方法. 示例: //初始化过程 ...

  6. inux 驱动程序开发中输入子系统总共能产生哪些事件类型(EV_KEY,EV_ABS,EV_REL)

    inux 驱动程序开发中, 输入子系统总共能产生哪些事件类型?,以及分别是什么意思?详见如下: Linux中输入设备的事件类型有EV_SYN 0x00 同步事件EV_KEY 0x01 按键事件,如KE ...

  7. <转>jmeter(八)断言

    本博客转载自:http://www.cnblogs.com/imyalost/category/846346.html 个人感觉不错,对jmeter讲解非常详细,担心以后找不到了,所以转发出来,留着慢 ...

  8. GM Bosch Vetronix HP Tech 2 with CANDI

    Being I have owned some of these units. Can offer some advice… GM Tech 1, GM Tech 1a. Early and latt ...

  9. 了解一下 Linux 上用于的 SSH 图形界面工具

    如果你碰巧喜欢好的图形界面工具,你肯定很乐于了解一些 Linux 上优秀的 SSH 图形界面工具.让我们来看看这三个工具,看看它们中的一个(或多个)是否完全符合你的需求. 在你担任 Linux 管理员 ...

  10. kivy中size和pos的使用

    kivy中位置和大小属性的使用: -------------------位置---------------------------- 1.pos_hint(‘x-axis-key’:value,’y- ...