|I was able to create a work around for my problem by starting each
| timer 1 second apart from each other. But I would still like to know
| how to do this without the workaround. Here is a code example:
|
| Thread thread = new Thread( new ThreadStart(MonitorThread) )
| thread.Start();
|
| private void MonitorThread()
| {
| MyObject obj1 = new MyObject();
| MyObject obj2 = new MyObject();
| MyObject obj3 = new MyObject();
| MyObject obj4 = new MyObject();
| MyObject obj5 = new MyObject();
| MyObject obj6 = new MyObject();
| }
|
| public class MyObject
| {
| public MyObject()
| {
| Timer timer = new Timer(x);
| timer.Elapsed += new ElapsedEventHandler(timerElapsed);
| timer.Start();
| }
| private void timerElapsed(object sender, ElapsedEventArgs e)
| {
| mutex.waitone();
| StreamWriter swLog = new StreamWriter(path, true);
| swLog.AutoFlush = true;
| swLog.WriteLine(DateTime.Now.ToString() + ": " + message);
| swLog.Close();
| mutex.ReleaseMutex();
| }
| }
|
| So basically each timer is a seperate thread. Those are the threads
| that im talking about. But each timer is started from the same thread.
| Help is appreciated.
|
Though I'm not clear on what you are trying to achieve in your design,
following is something I hacked together just to give you an idea of what
you can achieve using a simple Producer/Consumer queue.
Note that while the timers happen to run on a thread from the pool, they
don't necessarely run on different threads, all depends on the number of
CPU's in the box, the frequency of generated timer events and the service
time of the timer delegate, most of the time they are serviced by the same
couple of threads.
using System;
using System.IO;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
class Program
{
private static EventWaitHandle evntWh;
static void Main()
{
Program prog = new Program();
LoggerQueue<string> logMessageQueue = new LoggerQueue<string>();
evntWh = new EventWaitHandle(true, EventResetMode.ManualReset);
// start a consumer
Thread loggerThread = new Thread((ThreadStart) delegate {
prog.Consume(logMessageQueue); });
loggerThread.Start();
MyObject obj1 = new MyObject(logMessageQueue);
MyObject obj2 = new MyObject(logMessageQueue);
MyObject obj3 = new MyObject(logMessageQueue);
MyObject obj4 = new MyObject(logMessageQueue);
MyObject obj5 = new MyObject(logMessageQueue);
MyObject obj6 = new MyObject(logMessageQueue);
Console.ReadLine();
// Request Consumer thread to terminate
evntWh.Reset();
loggerThread.Join();
}
private void Consume(LoggerQueue<string> q) {
using (StreamWriter swLog = new StreamWriter("LogFile.txt")) {
swLog.AutoFlush = true;
string message;
// dequeu messages until requested to return
while (evntWh.WaitOne(100, false)) {
if((message = q.Dequeue()) != null)
swLog.WriteLine(message);
}
}
}
}
class MyObject
{
static int m_numMessagesPosted;
LoggerQueue<string> q;
System.Timers.Timer timer;
public MyObject(LoggerQueue<string> logMessageQueue) {
q = logMessageQueue;
Random r = new Random((int) DateTime.Now.Ticks);
timer = new System.Timers.Timer((double)r.Next(100, 2000));
timer.Elapsed += new System.Timers.ElapsedEventHandler(Produce);
timer.Start();
}
private void Produce(object sender, System.Timers.ElapsedEventArgs e) {
// enqueue a simple string message
string s = String.Format("Logmessage {0} threadId {1} - {2}",
Interlocked.Increment(ref m_numMessagesPosted),
Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString());
q.Enqueue(s);
}
}
class LoggerQueue<T>
{
private int _count = 0;
private Queue<T> _queue = new Queue<T>();
public T Dequeue()
{
lock (_queue)
{
while (_count <= 0)
{
// wait a maximum of x seconds, and return when no message queued so
far
if(!Monitor.Wait(_queue, 1000))
return default(T);
}
_count--;
return _queue.Dequeue();
}
}
public void Enqueue(T data)
{
if (data == null) throw new ArgumentNullException("data");
lock (_queue)
{
_queue.Enqueue(data);
_count++;
Monitor.PulseAll(_queue);
}
}
}
Willy.