-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
57 lines (50 loc) · 1.75 KB
/
Program.cs
File metadata and controls
57 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace WindowsService.MultiThread
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
var services = new List<IServiceThread>();
//add the services here that needs to run in each thread
services.Add(new FileWatcherService());
//running as service
if (!Environment.UserInteractive)
{
var servicesToRun = new ServiceBase[]
{
new MultiThreadService(services.ToArray()),
};
ServiceBase.Run(servicesToRun);
}
//running as console app
else
{
var workerThreads = new Thread[services.Count];
for (int i = 0; i < services.Count; i++)
{
services[i].ServiceStarted = true;
workerThreads[i] = new Thread(() => services[i].DoWork());
workerThreads[i].Start();
Console.WriteLine($"{services[i].GetType().Name} running in thread {i}");
}
Console.WriteLine("Service is running in console mode. Press any key to quit.");
Console.ReadKey();
for (int i = 0; i < services.Count; i++)
{
services[i].ServiceStarted = false;
workerThreads[i].Join(500);
}
}
}
}
}