边玩边学C#,调节显示器亮度源码,1分钟写一个小工具来调节网吧144Hz,244HZ电竞屏亮度-C#
监视系统某个服务,定时重启这个服务,用来避免某些系统服务运行久了容易出错导致系统一些功能不正常。这个是网吧网维需要的比较实用的功能之一。本源码就是为实现这功能而生。
本源码以监视马蹄游戏更新服务为例,实现每天凌晨02:00:00的时候重启该服务为演示代码。
可以举一反三简单修改下服务名称做成定时重启其他服务工具。
源码技术角度核心是操作系统服务和定时器Timer的使用。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace fixMtGmUpdateSrv { public partial class Form1 : Form { private String srvName = "xgamemgr64"; public static bool ISWindowsServiceInstalled(string serviceName) { try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { return true; } } return false; } catch { return false; } } /// <summary> /// 启动某个服务 /// </summary> /// <param name="serviceName"></param> public static void StartService(string serviceName) { try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30)); } } } catch { } } /// <summary> /// 停止某个服务 /// </summary> /// <param name="serviceName"></param> public static void StopService(string serviceName) { try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { service.Stop(); service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30)); } } } catch { } } /// <summary> /// 判断某个服务是否启动 /// </summary> /// <param name="serviceName"></param> public static bool ISStart(string serviceName) { bool result = true; try { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController service in services) { if (service.ServiceName == serviceName) { if ((service.Status == ServiceControllerStatus.Stopped) || (service.Status == ServiceControllerStatus.StopPending)) { result = false; } } } } catch { } return result; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Button button = (Button)sender; //设置当前按钮变灰色,不可点击,避免多次点击 button.Enabled = false; //停止服务 StopService(srvName); } private void button2_Click(object sender, EventArgs e) { //启动服务 Button button = (Button)sender; button.Enabled = false; StartService(srvName); } private void button3_Click(object sender, EventArgs e) { //重启服务 Button button = (Button)sender; button.Enabled = false; StopService(srvName); StartService(srvName); button.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { //DateTime.Now.ToLongTimeString().ToString(); // 20:16:16 //DateTime.Now.ToShortTimeString().ToString(); // 20:16 //DateTime.Now.ToString("hh:mm:ss"); // 08:05:57 //DateTime.Now.TimeOfDay.ToString(); // 20:33:50.7187500 //时间是02:00:00的时候就开始重启服务。 if (checkBox1.Checked) { if (DateTime.Now.ToString("hh:mm:ss") == "02:00:00") { StopService(srvName); StartService(srvName); } } button1.Enabled = ISStart(srvName); button2.Enabled = !button1.Enabled; } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Process.Start(new ProcessStartInfo("https://www.txwb.com") { UseShellExecute = true }); } } }
打开VS Studio 2022,新建一个C# .net winForm项目。
下一步,输入项目密码,选择4.7.2的框架(目前网吧系统用得多的框架)。
然后在窗口中加入一些必要的组件。
找到马蹄游戏更新服务的名称:
复制相关代码,并在代码里定义服务名称:
马蹄游戏更新服务定时重启工具C#源码,解决网吧马蹄更新不了游戏问题-前端开发