473,769 Members | 3,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot invoke worker thread in windows service

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.Threadin g namespace.

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

Many thanks.
Vikram.



Nov 16 '05 #1
1 2808
Vikram <vi************ @netdecisions.c om> 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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.ServiceP rocess;
using System.IO;
using System.Threadin g;

namespace WindowsService1
{
public class Service1 : System.ServiceP rocess.ServiceB ase
{

private System.Componen tModel.Containe r components = null;

public Service1()
{

InitializeCompo nent();

}
static void Main()
{
System.ServiceP rocess.ServiceB ase[] ServicesToRun;
ServicesToRun = new System.ServiceP rocess.ServiceB ase[] { new
Service1() };

System.ServiceP rocess.ServiceB ase.Run(Service sToRun);
}
private void InitializeCompo nent()
{
components = new System.Componen tModel.Containe r();
this.ServiceNam e = "Service1";
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
Thread th = new Thread(new ThreadStart(Thr eadThing));
th.Start();
}

private void ThreadThing()
{
StreamWriter sw = new StreamWriter(@" C:\test.out");
sw.WriteLine("T hat 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\
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8883
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form { mySocketClass sss; ...
5
8114
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker thread is aborted by the system. How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed? This is definitely happening in my application. I haven't read anything about this problem in...
3
1967
by: Zürcher See | last post by:
I'm using the following structure for my programm, and I never had problems. Now by the invoke of the MyObject_End eventhandler I get the following error: System.Windows.FormsSpecified cast is not valid. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object args) ....
6
31868
by: Peter Rilling | last post by:
Okay, I have the main thread which does all the work. This main thread spawns a worker thread that just periodically poles the environment looking for a certain condition. This second thread is very small, and only is responsible for raising a flag when the environment changes. What I want is for the main thread to raise an event when the change has been flagged. I other words, I want the main thread to raise the event, not this small...
6
4997
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing for long running reports. When the processing is complete it uses crystal reports to load a template file, populate it, and then export it to a PDF. It works fine so far....
3
1140
by: Jeff Stewart | last post by:
I have a worker thread running a POP3 mail retriever that raises a NewEmail event every time a message is retrieved. The NewMail handler updates a ListView in my form with a line representing the new message. I'm using Control.Invoke properly in the NewEmail handler, but I'm concerned about the worker thread sending more events than can be handled by the UI thread. There is a special sorting routine that runs every time the list is...
7
2129
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it waits for the worker thread to complete by means of a while t.isAlive, sleep(0) mechanism. But when my worker thread calls my UpdateProgressBar routine, which calls Me.Invoke, the invoke call blocks forever. But I can't figure out why the main...
6
2819
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second adds another dot to the label). This works great. However, when I put the GUI thread to sleep (Thread.Sleep), the thread seems to stall. At first I was expecting dots to still appear, but obviously as the GUI thread is asleep, they will not. ...
2
4424
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10050
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.