So i am trying to create a thread in the windows service but i'm getting an error 1607 when trying to execute it. Here's my code
protected override void OnStart(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
using (sw = File.AppendText(path))
{
sw.WriteLine("the service has started");
server = new Server(sw);
server.start();
start = true;
new Thread(() =>
{
Thread.CurrentThread.IsBackground = false;
/* run your code here */
while (start != true)
{
Thread.Sleep(2000);
}
// tcp ip server
server.started();
}).Start();
}
}
When i'm running it without thread, the OnStart program doesn't end until server ends. Basically when i right click the service and start, the server gets started, and i could connect client as well, but the status doesn't change to "running". It changes to running when the server code stops executing. Here's code for above.
protected override void OnStart(string[] args)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "Log.txt";
using (sw = File.AppendText(path))
{
sw.WriteLine("the service has started");
server = new Server(sw);
server.start();
start = true;
// running without thread
server.started();
}
}
This above code works, but after right clicking and hitting 'Start', status changes to "Running" after server code is done executing (when all clients leave and server shutsdown) not before that. I'm really new to windows service stuff so pardon any stupid mistakes.