我正在为工作创建一个内部不活动记录器,我们最初希望这是一个Windows窗体应用程序,其中隐藏的窗体形式在后台运行,但程序出现在任务管理器中,我们现在想要这个在Windows服务中使用的代码,我想知道这是否可能?如果是这样,我想帮助实现那个=]
这是应用程序的代码
using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using AccessEye;
using System.Linq;
using EasyNetQ;
using EasyNetQ.Topology;
using Microsoft.Win32;
using MySql.Data.MySqlClient;
using NLog;
using ProtoBuf;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Reflection;
namespace LogProgram
{
public partial class AppForm : Form
{
public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private Screensaver watcher;
public Inactivity inactivity;
IAdvancedBus bus;
IExchange exchange;
public AppForm()
{
InitializeComponent();
ConfigureForm();
// todo: should be in setting
int pollingInterval = 5000;
inactivity = new Inactivity(pollingInterval);
inactivity.Inactive += inactivity_Inactive;
inactivity.Active += inactivity_Active;
inactivity.InactivityThresholdMs = 5 * 1000; // todo: should be in setting
inactivity.Start();
watcher = new Screensaver(pollingInterval);
watcher.ScreensaverOff += watcher_ScreensaverOff;
watcher.ScreensaverOn += watcher_ScreensaverOn;
watcher.Start();
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
LogManager.ThrowExceptions = true;
// todo: connection string should be in setting
bus = RabbitHutch.CreateBus("host=as01.access.local;virtualHost=DEV-Reece;username=reece;password=reece").Advanced;
exchange = Exchange.DeclareTopic("UserActivity", true, false, null);
var fanout = Exchange.DeclareFanout("FanoutExchange", true, false, null);
fanout.BindTo(exchange, new[] { "#" });
}
public void ConfigureForm()
{
this.Hide();
TrayDisplayer.Visible = false;
}
public void WriteLogDataToDb(LogData data)
{
using (var db = new LogService.UserActivityDataContext())
{
DbLogData logData = AutoMapper.Mapper.Map<LogData, DbLogData>(data);
int t = (int)data.EventType;
EventType eventType = db.EventTypes.FirstOrDefault(r => r.Id == t);
if (eventType == null)
{
eventType = db.EventTypes.Add(new EventType
{
Event = GetEnumDescriptionAttributeValue(data.EventType),
Id = (int)data.EventType
});
db.SaveChanges();
}
logData.EventTypeId = eventType.Id;
db.LogEvents.Add(logData);
db.SaveChanges();
}
}
public static string GetEnumDescriptionAttributeValue(Enum value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
private void AppForm_Load(object sender, EventArgs e)
{
}
void watcher_ScreensaverOn(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = AccessEye.UserStateEvents.ScreensaverOn;
PublishLogData(data);
}
void watcher_ScreensaverOff(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = AccessEye.UserStateEvents.ScreensaverOff;
PublishLogData(data);
}
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
var data = LogDataFactory.CollectData();
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
data.EventType = UserStateEvents.Lock;
break;
case SessionSwitchReason.SessionUnlock:
data.EventType = UserStateEvents.Unlock;
break;
}
PublishLogData(data);
}
public void PublishLogData(AccessEye.LogData LogData)
{
WriteLogDataToDb(LogData);
if (!bus.IsConnected) return;
try
{
using (var publishChannel = bus.OpenPublishChannel())
{
publishChannel.Publish(exchange, LogData.EventType.ToString(), new Message<LogData>(LogData));
}
}
catch (EasyNetQException)
{
//todo: handle
}
}
public static byte[] Serialize<T>(T instance)
{
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, instance);
return stream.ToArray();
}
}
private static T DeSerialize<T>(byte[] data)
{
using (var stream = new MemoryStream(data))
{
return Serializer.Deserialize<T>(stream);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bus.Dispose();
}
public void inactivity_Active(object sender, EventArgs e)
{
inactivity.Stop();
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Active;
PublishLogData(data);
inactivity.Start();
}
public void inactivity_Inactive(object sender, EventArgs e)
{
inactivity.Stop();
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Inactive;
PublishLogData(data);
inactivity.Start();
}
public void SystemEvents_SessionEnding(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Logoff;
PublishLogData(data);
Logger.Trace("Logged off");
}
}
}
更新
错误1}预期C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 36 10 UserActivityMonitorBackGround 错误2方法必须具有返回类型C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 44 16 UserActivityMonitorBackGround 错误3预期的类,委托,枚举,接口或结构C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 230 28 UserActivityMonitorBackGround 错误4类型或命名空间定义,或预期的文件结束C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 233 5 UserActivityMonitorBackGround 错误5类型或命名空间名称&#39; Windows&#39;命名空间中不存在&#39;系统&#39; (您是否缺少程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 10 14 UserActivityMonitorBackGround 错误6类型或命名空间名称&#39; AccessEye&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 12 7 UserActivityMonitorBackGround 错误7类型或命名空间名称&#39; EasyNetQ&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 14 7 UserActivityMonitorBackGround 错误8类型或命名空间名称&#39; EasyNetQ&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 15 7 UserActivityMonitorBackGround 错误9类型或命名空间名称&#39; MySql&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 17 7 UserActivityMonitorBackGround 错误10类型或命名空间名称&#39; NLog&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 18 7 UserActivityMonitorBackGround 错误11类型或命名空间名称&#39; ProtoBuf&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 19 7 UserActivityMonitorBackGround 错误12&#39; UserActivityMonitorBackGround.Service1.Logger&#39;是一个&#39;字段&#39;但是像'&#39;类型&#39; C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 38 36 UserActivityMonitorBackGround 错误13名称&#39; LogManager&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 38 52 UserActivityMonitorBackGround 错误14类型或命名空间名称&#39;屏幕保护程序&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 39 17 UserActivityMonitorBackGround 错误15类型或命名空间名称&#39;不活动&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 40 16 UserActivityMonitorBackGround 错误16类型或命名空间名称&#39; IAdvancedBus&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 41 9 UserActivityMonitorBackGround 错误17类型或命名空间名称&#39; IExchange&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 42 9 UserActivityMonitorBackGround 错误18类型或命名空间名称&#39;不活动&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 53 34 UserActivityMonitorBackGround 错误19类型或命名空间名称&#39;屏幕保护程序&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 58 31 UserActivityMonitorBackGround 错误20名称&#39; LogManager&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 64 17 UserActivityMonitorBackGround 错误21名称&#39; RabbitHutch&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 66 23 UserActivityMonitorBackGround 错误22名称&#39; Exchange&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 67 28 UserActivityMonitorBackGround 错误23名称&#39; Exchange&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 68 30 UserActivityMonitorBackGround 错误24&#39; UserActivityMonitorBackGround.Service1&#39;不包含&#39;隐藏&#39;的定义没有扩展方法&#39;隐藏&#39;接受类型&#39; UserActivityMonitorBackGround.Service1&#39;的第一个参数。可以找到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 79 18 UserActivityMonitorBackGround 错误25名称&#39; TrayDisplayer&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 80 13 UserActivityMonitorBackGround 错误26类型或命名空间名称&#39; LogData&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 83 38 UserActivityMonitorBackGround 错误27类型或命名空间名称&#39; LogService&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 85 33 UserActivityMonitorBackGround 错误28类型或命名空间名称&#39; DbLogData&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 87 17 UserActivityMonitorBackGround 错误29名称&#39; AutoMapper&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 87 37 UserActivityMonitorBackGround 错误30类型或命名空间名称&#39; LogData&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 87 59 UserActivityMonitorBackGround 错误31类型或命名空间名称&#39; DbLogData&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 87 68 UserActivityMonitorBackGround 错误32类型或命名空间名称&#39; EventType&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 91 17 UserActivityMonitorBackGround 错误33类型或命名空间名称&#39; EventType&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 95 55 UserActivityMonitorBackGround 错误34名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 125 24 UserActivityMonitorBackGround 错误35名称&#39; AccessEye&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 126 30 UserActivityMonitorBackGround 错误36名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 132 24 UserActivityMonitorBackGround 错误37名称&#39; AccessEye&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 133 30 UserActivityMonitorBackGround 错误38名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 139 24 UserActivityMonitorBackGround 错误39名称&#39; UserStateEvents&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 144 38 UserActivityMonitorBackGround 错误40名称&#39; UserStateEvents&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 147 38 UserActivityMonitorBackGround 错误41类型或命名空间名称&#39; AccessEye&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 154 36 UserActivityMonitorBackGround 错误42类型或命名空间名称&#39; LogData&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 164 96 UserActivityMonitorBackGround 错误43类型或命名空间名称&#39;消息&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 164 88 UserActivityMonitorBackGround 错误44类型或命名空间名称&#39; EasyNetQException&#39;找不到(是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 167 20 UserActivityMonitorBackGround 错误45名称&#39; Serializer&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 177 17 UserActivityMonitorBackGround 错误46名称&#39; Serializer&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 186 24 UserActivityMonitorBackGround 错误47类型或命名空间名称&#39; FormClosingEventArgs&#39;找不到(你是否缺少using指令或程序集引用?)C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 190 55 UserActivityMonitorBackGround 错误48名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 199 24 UserActivityMonitorBackGround 错误49名称&#39; UserStateEvents&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 200 30 UserActivityMonitorBackGround 错误50名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 209 24 UserActivityMonitorBackGround 错误51名称&#39; UserStateEvents&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 210 30 UserActivityMonitorBackGround 错误52名称&#39; LogDataFactory&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 218 24 UserActivityMonitorBackGround 错误53名称&#39; UserStateEvents&#39;在当前上下文中不存在C:\ Users \ reece.cottam \ Documents \ Visual Studio 2010 \ OsWatch \ UserActivityMonitorBackGround \ Service1.cs 219 30 UserActivityMonitorBackGround
答案 0 :(得分:1)
否,这不会工作..
服务没有UI组件。因此,您无法在服务中显示form
。它不用于显示用户界面..
如果你想从任务栏隐藏你的winform,你可以这样做
yourForm.ShowInTaskBar=false;
你也可以从任务管理器隐藏你的winform,但我不知道如何实现它。你可以google for it ..