PC Review


Reply
Thread Tools Rate Thread

Cannot invoke worker thread in windows service

 
 
Vikram
Guest
Posts: n/a
 
      16th Feb 2005
Thanks Jonathan for the prompt reply.

We have developed a program in C#, originally a windows application, that
was invoking a worker thread. The threading mechanism works fine in this
case. The worker thread gets invoked and an excel file is created.

Currrently, we are trying to convert this program into a windows service. We
have removed any code that writes any kind of output. The service gets
created and we can start it without any error through the component
services. However the worker thread never gets invoked.

We created a totally new solution with a simple windows service project that
just invokes a thread. However, when we ran this service also, the thread
was never invoked. As a simple windows application, it worked.

We even tried using the timer class with the TimerCallback delegate in the
System.Threading namespace.

How can we get threading to work in a windows service?

Many thanks.
Vikram.









 
Reply With Quote
 
 
 
 
Jonathan Stowe
Guest
Posts: n/a
 
      16th Feb 2005
Vikram <(E-Mail Removed)> wrote:
>
> How can we get threading to work in a windows service?
>


Threading works in a Windows Service just as it does anywhere else - for
example the following (which is just the service template with a thread
added) works as expected:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Threading;

namespace WindowsService1
{
public class Service1 : System.ServiceProcess.ServiceBase
{

private System.ComponentModel.Container components = null;

public Service1()
{

InitializeComponent();

}


static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;


ServicesToRun = new System.ServiceProcess.ServiceBase[] { new
Service1() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}


private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}


protected override void OnStart(string[] args)
{
Thread th = new Thread(new ThreadStart(ThreadThing));
th.Start();
}

private void ThreadThing()
{
StreamWriter sw = new StreamWriter(@"C:\test.out");
sw.WriteLine("That worked!");
sw.Flush();
}


protected override void OnStop()
{

}
}
}

But more complicated examples are fine too, it might be possible that
the thread is running but it is not doing what it is supposed to be due
to permissions or something else. You probably want to add some code in
the thread method that makes some diagnostic output to either a file or
the event log or something.

/J\
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Control.Invoke call hangs worker thread =?Utf-8?B?Sm9l?= Microsoft Dot NET Framework Forms 3 27th Mar 2006 12:23 PM
SoapHttpClientProtocol.Invoke hangs when calling web service from worker thread -- only in debugger!? Gregory Hassett Microsoft C# .NET 1 26th Jul 2005 08:55 AM
Worker thread blocking on calls to .Invoke Jeff Stewart Microsoft VB .NET 6 29th Nov 2004 08:01 PM
worker thread shutdown and Form.Invoke Stephen Lamb Microsoft C# .NET 5 11th Oct 2004 04:29 PM
Is it necessary to use Invoke for User Controls from a Worker Thread? Charles Law Microsoft Dot NET 10 23rd May 2004 01:10 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:26 PM.