Peter said:
[...]
It would be rude to post it here, because it's a bit big, so please take
a
look at it here:
http://www.betasafe.com/code?view=deleginv
IMHO, it's rude _not_ to post the code here. Well, okay...maybe not so
much "rude" as "short-sighted".
This newsgroup is likely to be archived for some arbitrarily long period
of time. Who knows how long, but the current archives (the largest
repository I know of is now maintained by Google) go back decades. When
you post a link to a web site instead copying the code into the post
itself, you limit the lifetime of the usefulness of your post to that of
your web site. The message itself may live on longer than that (and it
probably will), but it won't be of any use to anyone else at that point.
When will your web site go defunct? One hopes not for a long time, but
it
could be tomorrow, next month, next year, whatever. Whatever the
lifetime, it's too short as compared to the likely lifetime of the
newsgroup.
More importantly, if you include the code in your post, the lifetime of
that copy of the code is tied perfectly to the newsgroup, which is
exactly
what one wants. Your article's lifetime should match the newsgroup's
lifetime, whether shorter or longer than your web site's, rather than
matching that of your web site.
Besides, while the code might be long, it's certainly no longer than the
huge number of top-posted, untrimmed articles other people post to the
newsgroup every day. I'm not saying two wrongs make a right, but in this
case I don't think it would be wrong to post the code, and in fact is
beneficial, and at the very least would help improve the signal-to-noise
ratio here (admittedly, already better than is found in many other
newsgroups, but still...)
Pete
Hi Pete,
You make some excellent points. I just thought it polite to at least let
people know that a large post would be coming... in the olden days it was
very impolite to post in HTML and/or include images due to peoples limited
connect speeds. I guess with the broadband revolution, I can archive my
code for a long time, like this:
Watch out for line wrapping!
///
using System;
using System.Collections.Generic;
using System.Threading;
public class ProcessorStopMessage : ProcessorMessage
{
public override string ToString ()
{
return "Processor stop message.";
}
}
public delegate void VoidDelegate ();
public class ProcessorInvokeMessage : ProcessorMessage
{
private VoidDelegate _deleg;
public ProcessorInvokeMessage (VoidDelegate deleg)
{
_deleg = deleg;
}
public VoidDelegate Delegate
{
get { return _deleg; }
}
public override string ToString ()
{
return "Delegate invocation message.";
}
}
public class ProcessorMessage
{
public override string ToString ()
{
return "No-op processor message.";
}
}
public class ProcessorContext
{
private int _id;
private ManualResetEvent _messageWaiting;
private Queue<ProcessorMessage> _messageQueue;
public ProcessorContext (int id)
{
_id = id;
_messageWaiting = new ManualResetEvent(false);
_messageQueue = new Queue<ProcessorMessage>();
}
public int ProcessorId
{
get { return _id; }
}
public void PushMessage (ProcessorMessage msg)
{
lock (_messageQueue) {
_messageQueue.Enqueue(msg);
_messageWaiting.Set();
}
}
public ProcessorMessage GetMessage ()
{
_messageWaiting.WaitOne();
lock (_messageQueue) {
ProcessorMessage msg = _messageQueue.Dequeue();
if (_messageQueue.Count == 0)
_messageWaiting.Reset();
return msg;
}
}
}
public class EntryPoint
{
public static void Main ()
{
EntryPoint ep = new EntryPoint();
ep.InternalMain();
}
private void InternalMain ()
{
ProcessorContext[] contexts = new ProcessorContext[5];
Thread[] processors = new Thread[5];
for (int i = 0; i < processors.Length; i++) {
contexts
= new ProcessorContext(i);
processors = new Thread(ProcessorThread);
processors.Start(contexts);
}
Console.WriteLine("All {0} processors have been started.",
processors.Length);
Console.WriteLine("Hit enter to invoke the TestDelegate on
processor
2");
Console.ReadLine();
contexts[2].PushMessage(new ProcessorInvokeMessage(TestDelegate));
Console.WriteLine("Hit enter to torture the processors.");
Console.ReadLine();
Random rng = new Random();
for (int i = 0; i < 1000; i++) {
contexts[rng.Next(contexts.Length)].PushMessage(new
ProcessorInvokeMessage(TestDelegate));
}
Console.WriteLine("Hit enter to stop the processors.");
Console.ReadLine();
for (int i = 0; i < processors.Length; i++) {
contexts.PushMessage(new ProcessorStopMessage());
}
}
private void ProcessorThread (object param)
{
ProcessorContext context = param as ProcessorContext;
while (true) {
ProcessorMessage msg = context.GetMessage();
Console.WriteLine("Processor {0} received a message:",
context.ProcessorId);
Console.WriteLine(msg.ToString());
if (msg is ProcessorStopMessage)
break;
else if (msg is ProcessorInvokeMessage)
((ProcessorInvokeMessage)msg).Delegate();
}
}
private void TestDelegate ()
{
Console.WriteLine("This delegate is being executed on thread {0}.",
Thread.CurrentThread.ManagedThreadId);
}
}
///