private void button2_Click(object sender, EventArgs e) { try { if (!Vali()) { return; } JackTime2Run.JackJob job = GeneJob(); JackTime2Run.NamePipeSrvClient client = new JackTime2Run.NamePipeSrvClient(); if (!client.UpdateJob(job)) { MessageBox.Show("执行失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { this.Close(); } } catch (Exception ex) { if (ex is System.ServiceModel.EndpointNotFoundException) { MessageBox.Show("连接失败,请检查是否启动了服务JackTime2Run!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("执行失败,失败信息:" + ex.ToString(), "执行失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private List <JackTime2Run.JackJob> GetAllTask() { JackTime2Run.NamePipeSrvClient client = new JackTime2Run.NamePipeSrvClient(); try { JackTime2Run.JackJob[] tasks = client.GetAllJobs(); if (tasks == null) { MessageBox.Show("拉取当前任务计划失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); checkBox1.Checked = false; return(new List <JackTime2Run.JackJob>()); } return(tasks.ToList()); } catch (Exception ex) { if (ex is System.ServiceModel.EndpointNotFoundException) { MessageBox.Show("拉取当前任务计划失败,请检查是否安装并启动了服务JackTime2Run!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show(ex.ToString()); } checkBox1.Checked = false; return(new List <JackTime2Run.JackJob>()); } }
/// <summary>获取完整的任务列表 /// </summary> /// <returns></returns> private List <JackTime2Run.JackJob> GetAllTask() { try { if (cycle_client.State == System.ServiceModel.CommunicationState.Faulted) { cycle_client = new JackTime2Run.NamePipeSrvClient(); } if (cycle_client.State != System.ServiceModel.CommunicationState.Opened) { cycle_client.Open(); } JackTime2Run.JackJob[] tasks = cycle_client.GetAllJobs(); //client.Close(); //成功返回结果,图标变正常 if (currentIcon == "disabled") { JackTime2Run.Icon = normal; currentIcon = "normal"; } if (tasks == null) { MessageBox.Show("拉取当前任务计划失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); checkBox1.Checked = false; return(new List <JackTime2Run.JackJob>()); } return(tasks.ToList()); } catch (Exception ex) { if (ex is System.ServiceModel.EndpointNotFoundException) { MessageBox.Show("拉取当前任务计划失败,请检查是否安装并启动了服务JackTime2Run!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show(ex.ToString()); } this.BeginInvoke((Action)(() => { //出现异常,图标变灰色 if (currentIcon == "normal") { JackTime2Run.Icon = disabled; currentIcon = "disabled"; } checkBox1.Checked = false; })); return(new List <JackTime2Run.JackJob>()); } }
/// <summary>单元格点击事件,启用、禁用、编辑以及删除功能代码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { if (dataGridView1.Columns[e.ColumnIndex] is DataGridViewButtonColumn) { string name = (dataGridView1.Rows[e.RowIndex].Cells[6].Value ?? "").ToString(); string value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); //这里可以编写你需要的任意关于按钮事件的操作 JackTime2Run.NamePipeSrvClient client = null; try { client = new JackTime2Run.NamePipeSrvClient(); if (value == "禁用") { if (MessageBox.Show("是否禁用任务:" + name + "?", "禁用任务:" + name, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { Task.Factory.StartNew(() => { if (!client.DisableJob(name)) { this.Invoke((Action)(() => { MessageBox.Show("执行失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); Refresh(); })); } ; }); } } else if (value == "启用") { if (MessageBox.Show("是否启用任务:" + name + "?", "启用任务:" + name, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { Task.Factory.StartNew(() => { if (!client.EnableJob(name)) { this.Invoke((Action)(() => { MessageBox.Show("执行失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); Refresh(); })); } ; }); } } else if (value == "删除") { if (MessageBox.Show("是否删除任务:" + name + "?", "删除任务:" + name, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { Task.Factory.StartNew(() => { if (!client.RemoveJob(name)) { this.Invoke((Action)(() => { MessageBox.Show("执行失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); Refresh(); })); } ; }); } } else if (value == "编辑") { int index = int.Parse(dataGridView1.Rows[e.RowIndex].Cells[5].Value.ToString()); DataTransfer.job = jobs[index - 1]; new JobEdit().ShowDialog(); Refresh(); } else if (value == "执行一次") { if (MessageBox.Show("是否临时执行任务:" + name + "?", "临时执行任务:" + name, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { Task.Factory.StartNew(() => { if (!client.TriJob(name)) { this.Invoke((Action)(() => { MessageBox.Show("执行失败,服务异常,请查看日志[" + AppDomain.CurrentDomain.BaseDirectory + "log\\SrvManage" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log]", "服务异常", MessageBoxButtons.OK, MessageBoxIcon.Warning); })); } ; }); } } } catch (Exception ex) { if (ex is System.ServiceModel.EndpointNotFoundException) { MessageBox.Show("连接失败,请检查是否启动了服务JackTime2Run!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("执行失败,失败信息:" + ex.ToString(), "执行失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } finally { client.Close(); } } } }