473,379 Members | 1,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Windows Service Threading

Dan
All,

I have a windows service that creates a couple of worker threads to do some processing. The service works fine.

However, the main service thread has a method for sending email notices that I would like to be able to call from the worker threads. Furthermore, I would like this call to be done asynchronously.

If I was working with a Windows Form app I would have no problem as the Form has a begin invoke method that works great for this. However, I can't find a similar method on the service.

How can I implement this scenario in a service application?

Thanks for the help!

Dan
Nov 16 '05 #1
4 9862
Create a work queue. When a thread wants to send an email, write the object
the queue and keep going. The email thread will block waiting on some
objects in the queue and pop them off and send them. Your worker could also
just call the thread pool passing the object which will call your delegate
to do the sending on another thread. I like the queue method, but what ever
works for you.

--
William Stacey, MVP

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
All,

I have a windows service that creates a couple of worker threads to do some processing. The service works fine.
However, the main service thread has a method for sending email notices that I would like to be able to call from the worker threads. Furthermore, I
would like this call to be done asynchronously.
If I was working with a Windows Form app I would have no problem as the Form has a begin invoke method that works great for this. However, I can't
find a similar method on the service.
How can I implement this scenario in a service application?

Thanks for the help!

Dan


Nov 16 '05 #2
Hi,

I also agree with the queue idea, it's VERY easy to implement, just
remember to Sync the queue, you do this by using Queue.Synchronized( )
This will make sure that the queue operations are thread safe.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Create a work queue. When a thread wants to send an email, write the object the queue and keep going. The email thread will block waiting on some
objects in the queue and pop them off and send them. Your worker could also just call the thread pool passing the object which will call your delegate
to do the sending on another thread. I like the queue method, but what ever works for you.

--
William Stacey, MVP

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
All,

I have a windows service that creates a couple of worker threads to do some processing. The service works fine.

However, the main service thread has a method for sending email notices

that I would like to be able to call from the worker threads. Furthermore,

I would like this call to be done asynchronously.

If I was working with a Windows Form app I would have no problem as the

Form has a begin invoke method that works great for this. However, I can't
find a similar method on the service.

How can I implement this scenario in a service application?

Thanks for the help!

Dan

Nov 16 '05 #3
:) I also like using a blocking queue so your thread blocks until some data
arrives. The default queue does not allow this behavior.

--
William Stacey, MVP

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Oc**************@TK2MSFTNGP10.phx.gbl...
Hi,

I also agree with the queue idea, it's VERY easy to implement, just
remember to Sync the queue, you do this by using Queue.Synchronized( )
This will make sure that the queue operations are thread safe.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"William Stacey [MVP]" <st***********@mvps.org> wrote in message
news:OG**************@tk2msftngp13.phx.gbl...
Create a work queue. When a thread wants to send an email, write the object
the queue and keep going. The email thread will block waiting on some
objects in the queue and pop them off and send them. Your worker could

also
just call the thread pool passing the object which will call your delegate to do the sending on another thread. I like the queue method, but what

ever
works for you.

--
William Stacey, MVP

"Dan" <Da*@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
All,

I have a windows service that creates a couple of worker threads to do

some processing. The service works fine.

However, the main service thread has a method for sending email
notices that I would like to be able to call from the worker threads. Furthermore, I
would like this call to be done asynchronously.

If I was working with a Windows Form app I would have no problem as

the Form has a begin invoke method that works great for this. However, I can't find a similar method on the service.

How can I implement this scenario in a service application?

Thanks for the help!

Dan



Nov 16 '05 #4
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
I also agree with the queue idea, it's VERY easy to implement, just
remember to Sync the queue, you do this by using Queue.Synchronized( )
This will make sure that the queue operations are thread safe.


Well, it makes sure that any individual queue operation is thread-safe.
It doesn't mean that any *sequence* of queue operations is thread-safe.
For instance, if you do:

if (queue.Count != 0)
{
return queue.Dequeue();
}

then another thread could dequeue between the Count test and the
Dequeue call, so you could end up with an InvalidOperationException
being thrown.

I personally prefer to do the locking myself, explicitly, in code - I
find it more obvious what's going on that way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

3
by: ray | last post by:
Hi, I just wrote a windows service application to call a function of another object periodically. I used System.Server.Timer and I found that it works fine within the first 2 hours but the...
2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
2
by: hnkien | last post by:
Hi, I am writing a windows service with threading.timer for 10 seconds but it didn't work. Here are my code: namespace SchedulerService { public class ScheduleService :...
1
by: Vikram | last post by:
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...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
7
by: Doug Stiers | last post by:
I have a VB app that I'm installing as a Windows Service. I want a subroutine in the app to run every 30 minutes during business hours. How do I do this in VB? I set the startup type as automatic...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
28
by: | last post by:
I have a multi threaded windows form application that runs great after calling Application.Run(). Application.Run is required for a COM component I a using in the app (required for message loop). ...
1
by: mdhaman | last post by:
hi, I have a windows service written in VB.Net and framework 2.0. It is a multithread service and it is using threadpool to manage threads. Recently I have started getting...
4
by: Steven De Smet | last post by:
Hello, This is my first post. I searched on the internet for answers but I was unable to solve my problem. So I hope that you guy's can help me with my VB.NET problem I tried to create a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.