473,461 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dispatcher Threads

Hello Group,

I'm pretty new to C-Sharp, so perhaps it's a well known problem and i
didn't get the right words to search for in google etc.

My Problem is: I wrote (well adopted it from Java) a Dispatcher Thread,
which is 1. a singleton and 2. it should be started from the main and
then wait for DispatcherTasks to work on.

A simple usage would be:

Dispatcher.start();
DispatcherTask loTask = new MyDispatcherTask(); // subclasses
DispatcherTask
Dispatcher.addTask(loTask);

To that point everything works well, but when the main finishes, the
Dispatcher Thread should finish too, but it is still waiting for new
Tasks. In Java it worked all right, but in C# the Dispatcher Thread
never finishes. I think it is all right, that it doesn't finish, but
that is not the intended use ... How do i configure the Dispatcher
Thread, that it finishes, when the Main finishes (perhaps it should
finish the work on the current Task, but finish afterwards)

Thx in advance, Nils Drews

using System;
using System.Threading;
using System.Collections;
using Framework.DispatcherSystem;
using NSpring.Logging;
namespace Framework.DispatcherSystem.Engine
{
/// <summary>
/// Summary description for Dispatcher.
/// </summary>
public class Dispatcher
{
private Logger logger =
Logger.CreateFileLogger("E:\\temp\\nspring.log");
private static Dispatcher singleton = null;
private Queue queue = new Queue();
private Thread thread = null;
private Dispatcher()
{
thread = new Thread(new ThreadStart(run));
}

public static Dispatcher getInstance()
{
if (singleton == null)
{
singleton = new Dispatcher();
}
return singleton;
}

public void start()
{
thread.Start();
}

private void run()
{
while (true)
{
while(queue.Count != 0)
{
DispatcherTask loTask = (DispatcherTask) queue.Dequeue();
try
{
loTask.execute();
} catch (Exception e)
{
logger.Open();
logger.Log(e);
}
}

lock (queue)
{
try
{
if (queue.Count == 0)
{
Monitor.Wait(queue);
}
}
catch (Exception e)
{
logger.Open();
logger.Log(e);
}
}
}
}

private void addTask(DispatcherTask poTask)
{
lock(queue)
{
queue.Enqueue(poTask);
Monitor.Pulse(queue);
}
}

public static void addDispatcherTask(DispatcherTask poTask)
{
getInstance().addTask(poTask);
}
}
}

Nov 17 '05 #1
1 13913
Hi,

Use Thread.IsBackground = true; for the worker thread
cheers,

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

<bu****@gmx.net> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
Hello Group,

I'm pretty new to C-Sharp, so perhaps it's a well known problem and i
didn't get the right words to search for in google etc.

My Problem is: I wrote (well adopted it from Java) a Dispatcher Thread,
which is 1. a singleton and 2. it should be started from the main and
then wait for DispatcherTasks to work on.

A simple usage would be:

Dispatcher.start();
DispatcherTask loTask = new MyDispatcherTask(); // subclasses
DispatcherTask
Dispatcher.addTask(loTask);

To that point everything works well, but when the main finishes, the
Dispatcher Thread should finish too, but it is still waiting for new
Tasks. In Java it worked all right, but in C# the Dispatcher Thread
never finishes. I think it is all right, that it doesn't finish, but
that is not the intended use ... How do i configure the Dispatcher
Thread, that it finishes, when the Main finishes (perhaps it should
finish the work on the current Task, but finish afterwards)

Thx in advance, Nils Drews

using System;
using System.Threading;
using System.Collections;
using Framework.DispatcherSystem;
using NSpring.Logging;
namespace Framework.DispatcherSystem.Engine
{
/// <summary>
/// Summary description for Dispatcher.
/// </summary>
public class Dispatcher
{
private Logger logger =
Logger.CreateFileLogger("E:\\temp\\nspring.log");
private static Dispatcher singleton = null;
private Queue queue = new Queue();
private Thread thread = null;
private Dispatcher()
{
thread = new Thread(new ThreadStart(run));
}

public static Dispatcher getInstance()
{
if (singleton == null)
{
singleton = new Dispatcher();
}
return singleton;
}

public void start()
{
thread.Start();
}

private void run()
{
while (true)
{
while(queue.Count != 0)
{
DispatcherTask loTask = (DispatcherTask) queue.Dequeue();
try
{
loTask.execute();
} catch (Exception e)
{
logger.Open();
logger.Log(e);
}
}

lock (queue)
{
try
{
if (queue.Count == 0)
{
Monitor.Wait(queue);
}
}
catch (Exception e)
{
logger.Open();
logger.Log(e);
}
}
}
}

private void addTask(DispatcherTask poTask)
{
lock(queue)
{
queue.Enqueue(poTask);
Monitor.Pulse(queue);
}
}

public static void addDispatcherTask(DispatcherTask poTask)
{
getInstance().addTask(poTask);
}
}
}

Nov 17 '05 #2

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

Similar topics

5
by: Frithiof Andreas Jensen | last post by:
Hi All, I am tinkering with a stock-market analysis program that needs to have (at least one) a worker thread for processing real-time data and a GUI where one can control/monitor the function...
2
by: Hollywood | last post by:
I have a system in which I have a single thread that places data on a Queue. Then I have one worker thread that waits until data is put on the thread and dequeues the Queue and processes that...
2
by: rodmc | last post by:
I have written an application which uses threads and the XMPP library - this is a library which supports the jabber instant messaging protocol. The application works fine when run from Idle,...
35
by: Carl J. Van Arsdall | last post by:
Alright, based a on discussion on this mailing list, I've started to wonder, why use threads vs processes. So, If I have a system that has a large area of shared memory, which would be better? ...
9
by: jdlists | last post by:
I have inheirted some existing code, that i will explain in a moment, have needed to extend and ultimately should be able to run in threads. I've done a bunch of work with python but very little...
0
by: egbert | last post by:
As an exercise in the use of dispatcher I concocted a Zoo with some Animals, see below. It works, but I think it is convoluted, obfuscated. The idea is that in the Zoo each animal needs its own...
2
by: Indy | last post by:
Greetings. I am writing an asynchronous server, and I use the standard library's module asyncore. I subclass asyncore.dispatcher. handle_accept works just right, that is, when a client socket...
7
by: miller.paul.w | last post by:
I've been doing some thinking, and I've halfway convinced myself of the following statement: that threads as implemented by Python (or Java) are exactly equivalent to one-shot continuations in...
0
by: sedefy | last post by:
i work in VC++ win32 application i want to creat 2 threads th1 and th2 th1 will run th2 and wait 10 second and increment a counter th2 will run 1 second and increment another counter during the 1...
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...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...
0
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...

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.