mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-15 14:11:03 +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://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/6490979/how-to-pass-parameters-to-windows-service
|
||||||
* - https://stackoverflow.com/questions/42812333/pass-an-argument-to-a-windows-service-at-automatic-startup
|
* - 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;
|
||||||
using System.ServiceProcess;
|
using System.ServiceProcess;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using MSScriptControl;
|
using MSScriptControl;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -46,6 +48,10 @@ namespace WelsonJS.Service
|
||||||
private bool disabledScreenTime = false;
|
private bool disabledScreenTime = false;
|
||||||
private ScreenMatching screenMatcher;
|
private ScreenMatching screenMatcher;
|
||||||
|
|
||||||
|
[DllImport("user32.dll")]
|
||||||
|
public static extern int GetSystemMetrics(int nIndex);
|
||||||
|
public static int SM_REMOTESESSION = 0x1000;
|
||||||
|
|
||||||
public ServiceMain(string[] args)
|
public ServiceMain(string[] args)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
@ -108,24 +114,13 @@ namespace WelsonJS.Service
|
||||||
defaultTimer.Elapsed += OnElapsedTime;
|
defaultTimer.Elapsed += OnElapsedTime;
|
||||||
timers.Add(defaultTimer);
|
timers.Add(defaultTimer);
|
||||||
|
|
||||||
// set screen timer
|
// check this session is the user interactive mode
|
||||||
if (!disabledScreenTime && Environment.UserInteractive) {
|
if (Environment.UserInteractive) {
|
||||||
screenMatcher = new ScreenMatching(workingDirectory);
|
this.OnUserInteractiveEnvironment();
|
||||||
|
|
||||||
Timer screenTimer = new Timer
|
|
||||||
{
|
|
||||||
Interval = 10000 // 10 seconds
|
|
||||||
};
|
|
||||||
screenTimer.Elapsed += OnScreenTime;
|
|
||||||
timers.Add(screenTimer);
|
|
||||||
|
|
||||||
Log("Screen Time Event Enabled");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
disabledScreenTime = true;
|
Log("Disabled the User Interactive Mode. (e.g., OnScreenTime)");
|
||||||
|
|
||||||
Log("Screen Time Event Disabled");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the log file path
|
// set the log file path
|
||||||
|
@ -144,7 +139,6 @@ namespace WelsonJS.Service
|
||||||
{
|
{
|
||||||
base.OnStart(args);
|
base.OnStart(args);
|
||||||
|
|
||||||
|
|
||||||
// check the script file exists
|
// check the script file exists
|
||||||
if (File.Exists(scriptFilePath))
|
if (File.Exists(scriptFilePath))
|
||||||
{
|
{
|
||||||
|
@ -201,6 +195,37 @@ namespace WelsonJS.Service
|
||||||
Log(appName + " Service Stopped");
|
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)
|
private void OnElapsedTime(object source, ElapsedEventArgs e)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|
Loading…
Reference in New Issue
Block a user