Window Service example (DotNet Framework 4.8)

Solgamahardik
2 min readDec 13, 2022

This blog is related to creating a window services application. Explain in detail how to implement window services with DotNet.

Step 1: Name your project and create the Project Window Service (.net Framework).

Step 2: You can go into the app.config file and put “appsetting” under the configuration tags.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings >
<add key="ThreadLine" value="1"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
</configuration>

Step 3: In service1.cs, go to the page, click the left menu, select view code (F7), and copy the code below.

[RunInstaller(true)]
public partial class Service1 : ServiceBase
{
int SchduleTime = Convert.ToInt16(ConfigurationSettings.AppSettings["ThreadLine"]);
public Thread worker = null;
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
try
{
ThreadStart start = new ThreadStart(Working);
worker = new Thread(start);
worker.Start();
}
catch (Exception)
{

throw;
}

}

public void Working()
{
while (true)
{
string path = "C:\\sample.txt";

using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("path => " + path);
writer.WriteLine(string.Format("Window Service Log2" + DateTime.Now.ToString()));
writer.Close();
}
Thread.Sleep(TimeSpan.FromMinutes(SchduleTime));
}
}

protected override void OnStop()
{
try
{
if ((worker != null) & worker.IsAlive)
{
worker.Abort();
}
}
catch (Exception)
{
throw;
}
}
}

Step 4: In that life menu, go to the service1.cs page and click “add installer.” Then it has two boxes, the first of which is ServiceProcessInstaller1 and the second of which is ServiceInstaller1.

You can start with the menu. Navigate to the property section using the left menu. Change the name of the account to localSystem. After that, you can go to the serviceinstaller1 property section and put in a service name like “MyService.” After that, build the project.

Step 5: Click on the window menu and type “cmd” (administrator. After that, type the command

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

install command

installutil.exe -i {{project bin path}}

example : installutil.exe -i C:\Users\*******\Desktop\Job\WindowService.exe

uninstall Command

installutil.exe -u {{project bin path}}

installutil.exe -u C:\Users\*******\Desktop\Job\WindowService.exe

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Solgamahardik
Solgamahardik

Written by Solgamahardik

👨‍💻 .NET developer with 7 years of experience, I thrive on crafting robust and efficient solutions that bridge the gap between ideas and implementation.

No responses yet