74 lines
1.8 KiB
C#
74 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.ServiceProcess;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Owin.Hosting;
|
|
using Ninject;
|
|
|
|
namespace WindowsDataCenter
|
|
{
|
|
public partial class Service1: ServiceBase
|
|
{
|
|
public Service1()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private IDisposable _webApp;
|
|
private bool _stopMainWorkerThread;
|
|
private Thread _mainWorkerThread;
|
|
|
|
public void Start()
|
|
{
|
|
OnStart(new string[] {});
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
OnStop();
|
|
}
|
|
|
|
protected override void OnStart(string[] args)
|
|
{
|
|
//Initialise the Ninject system.
|
|
var ninjectInstance = NinjectHelper.GetInstance();
|
|
|
|
_mainWorkerThread = new Thread(MainWorkerThread)
|
|
{
|
|
IsBackground = false,
|
|
Name = "OWIN SELF HOST MAIN THREAD"
|
|
};
|
|
//TODO: use app.config for endpoint config.
|
|
_webApp = WebApp.Start<StartOwin>("http://*:8800");
|
|
}
|
|
|
|
protected override void OnStop()
|
|
{
|
|
_webApp.Dispose();
|
|
_stopMainWorkerThread = true;
|
|
if (_mainWorkerThread != null && _mainWorkerThread.IsAlive)
|
|
{
|
|
_mainWorkerThread.Join(2000);
|
|
if (_mainWorkerThread.IsAlive)
|
|
{
|
|
_mainWorkerThread.Interrupt();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void MainWorkerThread()
|
|
{
|
|
while (!_stopMainWorkerThread)
|
|
{
|
|
Thread.Sleep(2000);
|
|
}
|
|
}
|
|
}
|
|
}
|