mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-12 04:31:04 +00:00
Update ServiceMain.cs
This commit is contained in:
parent
faf3d55d8d
commit
8bd2382973
|
@ -22,10 +22,12 @@
|
|||
* - https://learn.microsoft.com/en-us/dotnet/framework/windows-services/how-to-debug-windows-service-applications
|
||||
* - https://stackoverflow.com/questions/6490979/how-to-pass-parameters-to-windows-service
|
||||
* - https://stackoverflow.com/questions/42812333/pass-an-argument-to-a-windows-service-at-automatic-startup
|
||||
* - https://learn.microsoft.com/ko-kr/windows/win32/api/winuser/nf-winuser-getsystemmetrics
|
||||
*/
|
||||
using System;
|
||||
using System.ServiceProcess;
|
||||
using System.Timers;
|
||||
using System.Runtime.InteropServices;
|
||||
using MSScriptControl;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
|
@ -46,6 +48,10 @@ namespace WelsonJS.Service
|
|||
private bool disabledScreenTime = false;
|
||||
private ScreenMatching screenMatcher;
|
||||
|
||||
[DllImport("user32.dll")]
|
||||
public static extern int GetSystemMetrics(int nIndex);
|
||||
public static int SM_REMOTESESSION = 0x1000;
|
||||
|
||||
public ServiceMain(string[] args)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
@ -108,24 +114,13 @@ namespace WelsonJS.Service
|
|||
defaultTimer.Elapsed += OnElapsedTime;
|
||||
timers.Add(defaultTimer);
|
||||
|
||||
// set screen timer
|
||||
if (!disabledScreenTime && Environment.UserInteractive) {
|
||||
screenMatcher = new ScreenMatching(workingDirectory);
|
||||
|
||||
Timer screenTimer = new Timer
|
||||
{
|
||||
Interval = 10000 // 10 seconds
|
||||
};
|
||||
screenTimer.Elapsed += OnScreenTime;
|
||||
timers.Add(screenTimer);
|
||||
|
||||
Log("Screen Time Event Enabled");
|
||||
// check this session is the user interactive mode
|
||||
if (Environment.UserInteractive) {
|
||||
this.OnUserInteractiveEnvironment();
|
||||
}
|
||||
else
|
||||
{
|
||||
disabledScreenTime = true;
|
||||
|
||||
Log("Screen Time Event Disabled");
|
||||
Log("Disabled the User Interactive Mode. (e.g., OnScreenTime)");
|
||||
}
|
||||
|
||||
// set the log file path
|
||||
|
@ -144,7 +139,6 @@ namespace WelsonJS.Service
|
|||
{
|
||||
base.OnStart(args);
|
||||
|
||||
|
||||
// check the script file exists
|
||||
if (File.Exists(scriptFilePath))
|
||||
{
|
||||
|
@ -201,6 +195,37 @@ namespace WelsonJS.Service
|
|||
Log(appName + " Service Stopped");
|
||||
}
|
||||
|
||||
private void OnUserInteractiveEnvironment()
|
||||
{
|
||||
// check is it a remote desktop session
|
||||
if (GetSystemMetrics(SM_REMOTESESSION) > 0)
|
||||
{
|
||||
disabledScreenTime = true;
|
||||
Log("This application may not work correctly in a remote desktop session");
|
||||
}
|
||||
|
||||
// set screen timer
|
||||
if (!disabledScreenTime)
|
||||
{
|
||||
screenMatcher = new ScreenMatching(workingDirectory);
|
||||
|
||||
Timer screenTimer = new Timer
|
||||
{
|
||||
Interval = 10000 // 10 seconds
|
||||
};
|
||||
screenTimer.Elapsed += OnScreenTime;
|
||||
timers.Add(screenTimer);
|
||||
|
||||
Log("Screen Time Event Enabled");
|
||||
}
|
||||
else
|
||||
{
|
||||
disabledScreenTime = true;
|
||||
|
||||
Log("Screen Time Event Disabled");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnElapsedTime(object source, ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
|
|
Loading…
Reference in New Issue
Block a user