/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void DataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } if (this.dataGridView[e.ColumnIndex, e.RowIndex].OwningColumn.Name.Equals(ColumnNames.colSvcBtnStatus.ToString()) && e.RowIndex >= 0) { DataGridViewRow currentrow = dataGridView.CurrentRow; String svcName = Convert.ToString(currentrow.Cells[ColumnNames.colSvcName.ToString()].Value); String displayedSvcState = Convert.ToString(currentrow.Cells[ColumnNames.colSvcDisplay.ToString()].Value); MattimonAgentLibrary.Tools.MyServiceController.ServiceState querySvcState = MattimonAgentLibrary.Tools.MyServiceController.GetServiceStatus(svcName); // Update the grid if the sercice's state have actually changed // And do not trigger any ServiceStateChanged event. if (displayedSvcState.Equals(querySvcState.ToString())) { RefreshDataGridViewEntry(svcName); return; } // The service state is not actually updated here. // Let the Parent decide what to do with this by triggering a ServiceStateChanged event. if (ServiceStateChanged != null) { Delegate[] delegates = ServiceStateChanged.GetInvocationList(); foreach (ServiceStateChangedEventHandler handle in delegates) { handle(this, new ServiceStateChangedEventArgs(svcName)); } } } }
/// <summary> /// 启动监听器 /// </summary> public int Start() { int result = 1; try { if (BaseListener == null) { BaseListener = new HttpListener(); //提供一个简单的、可通过编程方式控制的 HTTP 协议侦听器。此类不能被继承。 //配置监听地址。+代表本机可能的IP,如localhost、127.0.0.1、192.168.199.X(本机IP)等,假如失败则以管理员方式启动 //string address = $"http://{IpAddress}:{Port}/web/"; string address = $"http://+:{Port}{Suffix}"; BaseListener.Prefixes.Clear(); BaseListener.Prefixes.Add(address); //定义url及端口号,通常设置为配置文件 BaseListener.Start(); Name = address; } //异步监听客户端请求,当客户端的网络请求到来时会自动执行Result委托 //该委托没有返回值,有一个IAsyncResult接口的参数,可通过该参数获取context对象 BaseListener.BeginGetContext(Result, null); _raiser.Run(); if (BaseListener.IsListening) { ServiceStateChanged?.BeginInvoke(this, new ServiceStateEventArgs($"HTTP监听服务{Name}已启动", ServiceState.Started), null, null); } } catch (Exception e) { LastErrorMessage = $"HTTP监听服务{Name}启动过程中出现错误:{e.Message}"; result = 0; Stop(); //throw; } return(result); }
/// <summary> /// 停止监听器 /// </summary> public int Stop() { int result = 1; try { if (BaseListener != null) { BaseListener.Stop(); BaseListener = null; } _raiser.Stop(); ServiceStateChanged?.BeginInvoke(this, new ServiceStateEventArgs($"HTTP监听服务{Name}已停止", ServiceState.Stopped), null, null); } catch (ObjectDisposedException e) { LastErrorMessage = $"HTTP监听服务{Name}已被释放:{e.Message}"; result = 0; BaseListener = null; } catch (Exception e) { LastErrorMessage = $"HTTP监听服务{Name}停止过程中出现错误:{e.Message}"; result = 0; throw; } return(result); }
protected virtual void OnServiceStateChanged(ServiceChangedEventArgs e) => ServiceStateChanged?.Invoke(this, e);
protected virtual void OnServiceStateChanged() { ServiceStateChanged?.Invoke(this, _serviceState); }