- Program.cs
MyService.cs - MyServiceInstaller.cs
Для этого я изменил Program.cs следующим образом:
Код: Выделить всё
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceProcess;
using System.IO;
using System.Diagnostics;
using System.Threading;
namespace MyService
{
class Program
{
public const string ServiceName = "MyService";
static void Main(string[] args)
{
//System.Diagnostics.Debugger.Launch();
if (Environment.UserInteractive)
{
// running as console app
Start(args);
Console.WriteLine("Main Console: Press any key to stop...");
Console.ReadKey(true);
Stop();
}
else
{
// running as service
using (var service = new Service())
{
ServiceBase.Run(service);
}
}
}
public static void Start(string[] args)
{
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("======================================{0} ", Environment.NewLine));
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("{0} started ", DateTime.Now));
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("-- Env.UserName:{0} -- Env.Version{1} --Console:{2}{3}", Environment.UserName, Environment.Version.ToString(),Environment.UserInteractive, Environment.NewLine));
Thread threadProcessAnalysis = new Thread(new ThreadStart(processAnalysis));
threadProcessAnalysis.Start();
//threadProcessAnalysis.Join();//thrade main asteapta thread sa se termine
//Console.WriteLine("...press a key from Start function...");
//Console.ReadKey();
}
public static void Stop()
{
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("{0} stopped{1}", DateTime.Now, Environment.NewLine));
}
static void processAnalysis()
{
Process[] processlist = Process.GetProcesses();
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("Lista Procese: {0} ", Environment.NewLine));
foreach (Process theprocess in processlist)
{
if (theprocess.MainWindowTitle.Length > 0)
{
//Console.WriteLine("--Process: {0} --ID: {1} --MainWindowsTitle: {2} --MWHandle: {3} {4}", theprocess.ProcessName, theprocess.Id, theprocess.MainWindowTitle, theprocess.MainWindowHandle);
File.AppendAllText(@"c:\temp\MyService.txt", String.Format("--Process: {0} --ID: {1} --MainWindowsTitle: {2} --MWHandle: {3} {4}", theprocess.ProcessName, theprocess.Id, theprocess.MainWindowTitle, theprocess.MainWindowHandle, Environment.NewLine));
}
}
}
}
}
Когда я запускаюсь как службы Windows, процессы, имеющие заголовок, не идентифицируются для записи в файл.
Почему результаты не в порядке при запуске в качестве службы Windows?
Подробнее здесь: https://stackoverflow.com/questions/786 ... onsole-app