Skip to content

Commit 80785e6

Browse files
authored
Add files via upload
1 parent bd698e3 commit 80785e6

11 files changed

+414
-0
lines changed

App.config

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
<appSettings>
7+
<add key="WatchPath" value="C:\Temp"/>
8+
</appSettings>
9+
</configuration>

FileWatcherService.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Configuration;
3+
using System.IO;
4+
using System.Threading;
5+
6+
namespace WindowsService.MultiThread
7+
{
8+
class FileWatcherService:IServiceThread
9+
{
10+
private void ProcessFile(object sender, FileSystemEventArgs e)
11+
{
12+
Console.WriteLine($"{e.ChangeType}, {e.Name}");
13+
}
14+
15+
public bool ServiceStarted { get; set; }
16+
public void DoWork()
17+
{
18+
if (ServiceStarted)
19+
{
20+
var watchPath = ConfigurationManager.AppSettings["WatchPath"];
21+
var watcher = new FileSystemWatcher(watchPath) { EnableRaisingEvents = true };
22+
watcher.Filter = "*.*";
23+
watcher.Created += ProcessFile;
24+
watcher.Deleted += ProcessFile;
25+
watcher.Changed += ProcessFile;
26+
}
27+
else
28+
{
29+
Thread.CurrentThread.Abort();
30+
}
31+
}
32+
}
33+
}

IServiceThread.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace WindowsService.MultiThread
8+
{
9+
public interface IServiceThread
10+
{
11+
bool ServiceStarted { get; set; }
12+
void DoWork();
13+
}
14+
}

MultiThreadService.Designer.cs

+37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MultiThreadService.cs

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Data;
5+
using System.Diagnostics;
6+
using System.Linq;
7+
using System.ServiceProcess;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
namespace WindowsService.MultiThread
13+
{
14+
public partial class MultiThreadService : ServiceBase
15+
{
16+
private Thread[] _workerThreads;
17+
private readonly IServiceThread[] _serviceThreads;
18+
readonly int _numberOfThreads;
19+
public MultiThreadService(IServiceThread[] serviceThreads)
20+
{
21+
_serviceThreads = serviceThreads;
22+
_numberOfThreads = serviceThreads.Length;
23+
24+
InitializeComponent();
25+
}
26+
27+
protected override void OnStart(string[] args)
28+
{
29+
_workerThreads = new Thread[_numberOfThreads];
30+
31+
for (var i = 0; i < _numberOfThreads; i++)
32+
{
33+
_serviceThreads[i].ServiceStarted = true;
34+
_workerThreads[i] = new Thread(() => _serviceThreads[i].DoWork());
35+
_workerThreads[i].Start();
36+
}
37+
}
38+
39+
protected override void OnStop()
40+
{
41+
for (int i = 0; i < _numberOfThreads; i++)
42+
{
43+
_serviceThreads[i].ServiceStarted = false;
44+
_workerThreads[i].Join(500);
45+
}
46+
}
47+
}
48+
}

Program.cs

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

Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("WindowsService.MultiThread")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("Advent Software, Inc.")]
12+
[assembly: AssemblyProduct("WindowsService.MultiThread")]
13+
[assembly: AssemblyCopyright("Copyright © Advent Software, Inc. 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("2bbd35c4-1bf0-4048-9e0e-b46dcdb585ea")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

ServiceInstaller.Designer.cs

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ServiceInstaller.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Configuration.Install;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
9+
namespace WindowsService.MultiThread
10+
{
11+
[RunInstaller(true)]
12+
public partial class ServiceInstaller : System.Configuration.Install.Installer
13+
{
14+
public ServiceInstaller()
15+
{
16+
InitializeComponent();
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)