VS2015 定时服务及控制端

 

一.  服务端

  1. 如下图—新建项目—经典桌面—Windows服务—起名svrr

         

 

        2. 打到server1 改名为svrExecSqlInsert 右击对应的设计界面,添加安装服务目录结构如图

           

        3. svrExecSqlInsert里有打到OnStart()方法开始写代码如下 

        /// <summary>/// 服务开启操作/// </summary>/// <param name="args"></param>protected override void OnStart(string[] args){Timer tmInsert = new Timer();tmInsert.Interval = 5000;//5秒的定时器tmInsert.Elapsed += TmInsert_Elapsed;tmInsert.Enabled = true;}/// <summary>/// 定时执行数据库插入/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void TmInsert_Elapsed(object sender, ElapsedEventArgs e){using (SqlConnection con = new SqlConnection("server=.;uid=sa;pwd=123;database=oa")){using (SqlCommand cmd = new SqlCommand()){con.Open();cmd.Connection = con;cmd.CommandText = @"insert into UserInfo(UserName,Password,dutyid,RegTime,email)values('test','123',0,GETDATE(),'a@b.com')";cmd.CommandType = CommandType.Text;cmd.ExecuteNonQuery();}}}

到现在服务端就完成了 选通过命令安装后 看看数据库中的变化 已经生成了很多数据了

            

二.  服务控制端 有时我们同一个服务可能通过配置文件等对不同实例的数据库进行操作而服务又要安在同一台电脑上,不要说用的人员了就是.net开发的人员对这一些命令也有点头大,那么就有了控制断,这个先建一个窗体应用程序

          

         1. 将form1改为frmMain 放一个menuStrip 管理服务(安装、卸载、刷新)、操作服务(启动、停止)、退出 再加一下dataGridView Dock=fill

         2. 增加一个ServiceInfo类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace SvrUI
{public class ServerInfo{public string SvrName { get; set; }public string SvrDisplayName { get; set; }public string SvrDescription { get; set; }public string SvrPath { get; set; }}
}

3在frmMain窗体中加入如下代码来显示计算机中的服务列表 如果要显示自己安装的服务可通过安半时限制服务前缀或后缀过滤一下

 public frmMain(){InitializeComponent();IniControls();IniData();}/// <summary>///1. 添加控件/// </summary>private void IniControls(){DataGridViewCell cellTemple = new DataGridViewTextBoxCell();dataGridView1.Columns.AddRange(new DataGridViewColumn[] {new DataGridViewTextBoxColumn() { Name="SvrName",HeaderText="服务名称",DataPropertyName="SvrName",CellTemplate=cellTemple },new DataGridViewTextBoxColumn() { Name="SvrDisplayName",HeaderText="服务显示名称",DataPropertyName="SvrDisplayName",CellTemplate=cellTemple},new DataGridViewTextBoxColumn() { Name="SvrDescription",HeaderText="服务描述名称",DataPropertyName="SvrDescription",CellTemplate=cellTemple},new DataGridViewTextBoxColumn() { Name="SvrStatus",HeaderText="服务状态",DataPropertyName="SvrStatus",CellTemplate=cellTemple},new DataGridViewTextBoxColumn() { Name="SvrPath",HeaderText ="服务路径",DataPropertyName="SvrPath",CellTemplate=cellTemple,AutoSizeMode= DataGridViewAutoSizeColumnMode.Fill}});}/// <summary>///2. 初始化数据/// </summary>private void IniData(){//显示服务列表 如果只控制自己安装的服务 可以在安装时限制显示的名称 如显示名称必须包含MySvr:// var svrs = ServiceController.GetServices().Where(svr=>svr.DisplayName.Contains("MySvr"));var svrs = ServiceController.GetServices();//显示电脑的全部服务
List<ServiceInfo> lst = new List<ServiceInfo>();foreach (var svr in svrs){string description = "";string cpath = GetWindowsServiceInstallPath(svr.ServiceName, out description);lst.Add(new ServiceInfo() { SvrName = svr.ServiceName, SvrDisplayName = svr.DisplayName, SvrStatus = svr.Status.ToString(), SvrDescription = description, SvrPath = cpath });}dataGridView1.DataSource = lst;}/// <summary>/// 获取服务安装路径/// </summary>/// <param name="ServiceName"></param>/// <returns></returns>public string GetWindowsServiceInstallPath(string ServiceName, out string description){string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();description = (Registry.LocalMachine.OpenSubKey(key).GetValue("Description") ?? "").ToString();//替换掉双引号   path = path.Replace("\"", string.Empty);return path;}private void 退出ToolStripMenuItem_Click(object sender, EventArgs e){Close();}
View Code

效果如图:

其他代码待续。。。。

 

转载于:https://www.cnblogs.com/SoftWareIe/p/9732760.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388347.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

css文件如何设置scss,Webpack - 如何将scss编译成单独的css文件?

2 个答案:答案 0 :(得分&#xff1a;3)这是我在尝试将css编译成单独文件时使用的webpack.config.js文件|-- App|-- dist|-- src|-- css|-- header.css|-- sass|-- img|-- partials|-- _variables.scss|-- main.scss|--ts|-- tsconfig.json|-- user.ts|-- main.js|-- app.js|-- …

Iphone表视图的简单操作

1.创建一个Navigation—based—Application项目&#xff0c;这样Interface Builder中会自动生成一个Table View&#xff0c;然后将Search Bar拖放到表示图上&#xff0c;以我们要给表示图添加搜索功能&#xff0c;不要忘记将Search Bar的delegate连接到File‘s Owner项&#xf…

PhantomJS的使用

PhantomJS安装下载地址 配置环境变量 成功&#xff01; 转载于:https://www.cnblogs.com/hankleo/p/9736323.html

aws emr 大数据分析_DataOps —使用AWS Lambda和Amazon EMR的全自动,低成本数据管道

aws emr 大数据分析Progression is continuous. Taking a flashback journey through my 25 years career in information technology, I have experienced several phases of progression and adaptation.进步是连续的。 在我25年的信息技术职业生涯中经历了一次闪回之旅&…

21eval 函数

eval() 函数十分强大 ---- 将字符串 当成 有效的表达式 来求职 并 返回计算结果 1 # 基本的数学计算2 print(eval("1 1")) # 23 4 # 字符串重复5 print(eval("* * 5")) # *****6 7 # 将字符串转换成列表8 print(eval("[1, 2, 3, 4]")) # [1,…

联想r630服务器开启虚拟化,整合虚拟化 联想万全R630服务器上市

虚拟化技术的突飞猛进&#xff0c;对运行虚拟化应用的服务器平台的运算性能提出了更高的要求。近日&#xff0c;联想万全R630G7正式对外发布。这款计算性能强劲&#xff0c;IO吞吐能力强大的四路四核服务器&#xff0c;主要面向高端企业级应用而开发。不仅能够完美承载大规模数…

Iphone屏幕旋转

该示例是想在手机屏幕方向发生改变时重新定位视图&#xff08;这里是一个button&#xff09; 1.创建一个View—based Application项目,并在View窗口中添加一个Round Rect Button视图&#xff0c;通过尺寸检查器设置其位置&#xff0c;然后单击View窗口右上角的箭头图标来旋转窗…

先进的NumPy数据科学

We will be covering some of the advanced concepts of NumPy specifically functions and methods required to work on a realtime dataset. Concepts covered here are more than enough to start your journey with data.我们将介绍NumPy的一些高级概念&#xff0c;特别是…

lsof命令详解

基础命令学习目录首页 原文链接&#xff1a;https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316599.html 简介 lsof(list open files)是一个列出当前系统打开文件的工具。在linux环境下&#xff0c;任何事物都以文件的形式存在&#xff0c;通过文件不仅仅可以访问常规…

Xcode中捕获iphone/ipad/ipod手机摄像头的实时视频数据

目的&#xff1a;打开、关闭前置摄像头&#xff0c;绘制图像&#xff0c;并获取摄像头的二进制数据。 需要的库 AVFoundation.framework 、CoreVideo.framework 、CoreMedia.framework 、QuartzCore.framework 该摄像头捕抓必须编译真机的版本&#xff0c;模拟器下编译不了。 函…

统计和冰淇淋

Photo by Irene Kredenets on UnsplashIrene Kredenets在Unsplash上拍摄的照片 摘要 (Summary) In this article, you will learn a little bit about probability calculations in R Studio. As it is a Statistical language, R comes with many tests already built in it, …

信息流服务器哪种好,选购存储服务器需要注意六大关键因素,你知道几个?

原标题&#xff1a;选购存储服务器需要注意六大关键因素&#xff0c;你知道几个&#xff1f;信息技术的飞速发展带动了整个信息产业的发展。越来越多的电子商务平台和虚拟化环境出现在企业的日常应用中。存储服务器作为企业建设环境的核心设备&#xff0c;在整个信息流中承担着…

t3 深入Tornado

3.1 Application settings 前面的学习中&#xff0c;在创建tornado.web.Application的对象时&#xff0c;传入了第一个参数——路由映射列表。实际上Application类的构造函数还接收很多关于tornado web应用的配置参数。 参数&#xff1a; debug&#xff0c;设置tornado是否工作…

vml编辑器

<HTML xmlns:v> <HEAD> <META http-equiv"Content-Type" content"text/html; Charsetgb2312"> <META name"GENERATOR" content"网络程序员伴侣(Lshdic)2004"> <META name"GENERATORDOWNLOADADDRESS&q…

对数据仓库进行数据建模_确定是否可以对您的数据进行建模

对数据仓库进行数据建模Some data sets are just not meant to have the geospatial representation that can be clustered. There is great variance in your features, and theoretically great features as well. But, it doesn’t mean is statistically separable.某些数…

15 并发编程-(IO模型)

一、IO模型介绍 1、阻塞与非阻塞指的是程序的两种运行状态 阻塞&#xff1a;遇到IO就发生阻塞&#xff0c;程序一旦遇到阻塞操作就会停在原地&#xff0c;并且立刻释放CPU资源 非阻塞&#xff08;就绪态或运行态&#xff09;&#xff1a;没有遇到IO操作&#xff0c;或者通过某种…

arduino消息服务器,在C(Arduino IDE)中将API链接消息解析为服务器(示例代码)

我正在使用Arduino IDE来编程我的微控制器&#xff0c;它有一个内置的Wi-Fi芯片(ESP8266 NodeMCU)&#xff0c;它连接到我的互联网路由器&#xff0c;然后有一个特定的IP(就像192.168.1.5)。所以我想通过添加到链接的消息发送命令(和数据)&#xff0c;然后链接变为&#xff1a;…

不提拔你,就是因为你只想把工作做好

2019独角兽企业重金招聘Python工程师标准>>> 我有个朋友&#xff0c;他30出头&#xff0c;在500强公司做技术经理。他戴无边眼镜&#xff0c;穿一身土黄色的夹克&#xff0c;下面是一条常年不洗的牛仔裤加休闲皮鞋&#xff0c;典型技术高手范。 三 年前&#xff0c;…

python内置函数多少个_每个数据科学家都应该知道的10个Python内置函数

python内置函数多少个Python is the number one choice of programming language for many data scientists and analysts. One of the reasons of this choice is that python is relatively easier to learn and use. More importantly, there is a wide variety of third pa…

C#使用TCP/IP与ModBus进行通讯

C#使用TCP/IP与ModBus进行通讯1. ModBus的 Client/Server模型 2. 数据包格式及MBAP header (MODBUS Application Protocol header) 3. 大小端转换 4. 事务标识和缓冲清理 5. 示例代码 0. MODBUS MESSAGING ON TCP/IP IMPLEMENTATION GUIDE 下载地址&#xff1a;http://www.modb…