C# 服务程序 弹窗

1.创建C#服务程序

首先,创建C#服务程序,打开VS2017,添加新项目,选择windows服务,并根据需要命名,如:MyService,确定

在这里插入图片描述

如图,切换到代码块

在这里插入图片描述

2.初始代码

只有开始和结束函数

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace MyService
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
        }

        protected override void OnStop()
        {
        }
    }
}

3.添加安装程序

右键步骤2图片空白处,添加安装程序

在这里插入图片描述

在这里插入图片描述

4.设置属性

首先鼠标左键点击serviceProcessInstaller1,查看右下角的属性,并进行编辑,此处需要将Account更改为LocalSystem,这个方式权限比较高。另外用户可以自定义Name
在这里插入图片描述

然后鼠标左键点击serviceInstaller1,查看右下角的属性,并进行编辑,用户可以编辑服务的名称ServiceName、显示名称DisplayName、描述Description等属性。

在这里插入图片描述

设置弹窗,右键项目添加类

在这里插入图片描述

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MyService
{
    class Interop
    {

		        public static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
		        public static void ShowMessageBox(string message, string title)
		        {
		            int resp = 0;
		            WTSSendMessage(
		                WTS_CURRENT_SERVER_HANDLE,
		                WTSGetActiveConsoleSessionId(),
		                title, title.Length,
		                message, message.Length,
		                0, 0, out resp, false);
		        }
		
		        [DllImport("kernel32.dll", SetLastError = true)]
		        public static extern int WTSGetActiveConsoleSessionId();
		
		        [DllImport("wtsapi32.dll", SetLastError = true)]
		        public static extern bool WTSSendMessage(
		            IntPtr hServer,
		            int SessionId,
		            String pTitle,
		            int TitleLength,
		            String pMessage,
		            int MessageLength,
		            int Style,
		            int Timeout,
		            out int pResponse,
		            bool bWait
		        );
    }
}

在主函数里调用

在这里插入图片描述
Interop.ShowMessageBox(“This a message from AlertService.”, “AlertService Message”);
至此C#服务程序配置完成

5.服务的发布和卸载

注册:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe 工程路径\bin\Debug\WmsWService.exe
注销:C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /u 工程路径\bin\Debug\WmsWService.exe
WIN+R输入cmd打开命令行进行注册

在这里插入图片描述

WIN+R输入services.msc查看服务

在这里插入图片描述

右键启动服务

在这里插入图片描述
弹窗出现

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猪悟道

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值