B
Bill McCormick
Hello,
I'd like to bounce some things off developers and MSDN folks that are here.
I have a Windows service MyService that is the main application. It will
build a MyWorkObject in it's constructor and the OnStart and OnStop
methods set a property that will start/stop a worker thread or threads.
The worker thread(s) will process a queue and the dequeued item will be
processed by some asynchronous process.
Just a few questions for starters:
1. Is the general framework of this OK? That is, the service constructor
creates an object and OnStart/OnStop methods start/stop worker threads?
2. If garbage collection is no panacea, what questions do I need to ask
to determine the disposal process of MyObject and objects that it creates?
3. What threading design should I use to handle the
myWorkQueueX.Process() so that it will block until there is an item in
the queue to process and the current item is finished being processed?
Is this an application for some sort of ThreadPool construct?
The code below is an abstraction of a rough 1st attempt and included to
facilitate discussion. All comments, ideas and criticism are welcome.
Thanks,
Bill
//Project MyNamespace1.csproj
//Source: MyNamespace1.cs
//Creates: MyNamespace1.exe
using MyNamespace2
namespace MyNamespace1 {
public partial class MyService : ServiceBase
{
private MyWorkObject myWorkObject;
public MyService()
{
InitializeComponent();
myWorkObject = new MyWorkObject();
}
protected override void OnStart(string[] args)
{
myWorkObject.MyWorkerThreadRun = true;
}
protected override void OnStop()
{
myWorkObject.MyWorkerThreadRun = false;
}
}
}
//Project MyNamespace2.csproj
//Source: MyNamespace2.cs
//Creates: MyNamespace2.dll
namespace MyNamespace2 {
public class MyWorkObject {
private static MyWorkQueue1 myWorkQueue1;
private static MyWorkQueue2 myWorkQueue2;
private Thread myWorkerThread;
private bool myWorkerThreadRunning;
private bool myWorkerThreadRun;
public bool MyWorkerThreadRun {
set {
myWorkerThreadRun = value;
if (myWorkerThreadRun && !myWorkerThreadRunning)
myWorkerThread.Start();
}
}
public MyObject() {
myWorkerThread = new Thread(this.myWorker);
}
private void myWorker() {
myWorkQueue1 = new MyWorkQueue1();
myWorkQueue2 = new MyWorkQueue2();
try {
while (myWorkerThreadRun) {
if (!myWorkerThreadRunning) {
myWorkerThreadRunning = true;
}
myWorkQueue1.Process();
myWorkQueue2.Process();
Thread.Sleep(20);
}
} catch (Exception ex) {
//do some logging
} finally {
myWorkerThreadRunning = false;
}//end try
}//end myWorker
}//end MyWorkObject
}//end MyNamespace2
I'd like to bounce some things off developers and MSDN folks that are here.
I have a Windows service MyService that is the main application. It will
build a MyWorkObject in it's constructor and the OnStart and OnStop
methods set a property that will start/stop a worker thread or threads.
The worker thread(s) will process a queue and the dequeued item will be
processed by some asynchronous process.
Just a few questions for starters:
1. Is the general framework of this OK? That is, the service constructor
creates an object and OnStart/OnStop methods start/stop worker threads?
2. If garbage collection is no panacea, what questions do I need to ask
to determine the disposal process of MyObject and objects that it creates?
3. What threading design should I use to handle the
myWorkQueueX.Process() so that it will block until there is an item in
the queue to process and the current item is finished being processed?
Is this an application for some sort of ThreadPool construct?
The code below is an abstraction of a rough 1st attempt and included to
facilitate discussion. All comments, ideas and criticism are welcome.
Thanks,
Bill
//Project MyNamespace1.csproj
//Source: MyNamespace1.cs
//Creates: MyNamespace1.exe
using MyNamespace2
namespace MyNamespace1 {
public partial class MyService : ServiceBase
{
private MyWorkObject myWorkObject;
public MyService()
{
InitializeComponent();
myWorkObject = new MyWorkObject();
}
protected override void OnStart(string[] args)
{
myWorkObject.MyWorkerThreadRun = true;
}
protected override void OnStop()
{
myWorkObject.MyWorkerThreadRun = false;
}
}
}
//Project MyNamespace2.csproj
//Source: MyNamespace2.cs
//Creates: MyNamespace2.dll
namespace MyNamespace2 {
public class MyWorkObject {
private static MyWorkQueue1 myWorkQueue1;
private static MyWorkQueue2 myWorkQueue2;
private Thread myWorkerThread;
private bool myWorkerThreadRunning;
private bool myWorkerThreadRun;
public bool MyWorkerThreadRun {
set {
myWorkerThreadRun = value;
if (myWorkerThreadRun && !myWorkerThreadRunning)
myWorkerThread.Start();
}
}
public MyObject() {
myWorkerThread = new Thread(this.myWorker);
}
private void myWorker() {
myWorkQueue1 = new MyWorkQueue1();
myWorkQueue2 = new MyWorkQueue2();
try {
while (myWorkerThreadRun) {
if (!myWorkerThreadRunning) {
myWorkerThreadRunning = true;
}
myWorkQueue1.Process();
myWorkQueue2.Process();
Thread.Sleep(20);
}
} catch (Exception ex) {
//do some logging
} finally {
myWorkerThreadRunning = false;
}//end try
}//end myWorker
}//end MyWorkObject
}//end MyNamespace2