المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : need help with adding a worker thread to my singleton



C# Programming
11-05-2009, 05:11 PM
purpose
I am implementing a singleton "message queue". Several threads will enqueue messages to this queue and the worker thread will dequeue messages and process the message.

problem
in the line: "instance.threadWorker = new Thread(new ThreadStart(MessageWorker));" causes the error...
"An object reference is required for the non-static field, method, or property 'Application_Messaging.class_MessageQueue.MessageWorker()'"


code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

#pragma warning disable 0628 //suppress "new protected member declared in sealed class"

namespace Application_Messaging
{
//singleton class because we only want one message queue
// note: A sealed class cannot be inherited
public sealed class class_MessageQueue
{

private static class_MessageQueue instance = null;
private static readonly object padlock = new object();
private Thread threadWorker;

//this is a singleton, only the Instance property may be used
protected class_MessageQueue()
{
}

//property to get an instance to the class
public static class_MessageQueue Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
instance = new class_MessageQueue();

instance.threadWorker = new Thread(new ThreadStart(MessageWorker));
instance.threadWorker.Start();
}
return instance;
}
}
}

private void MessageWorker()
{
//will be a loop to dequeue messages
}


}//end of: class class_MessageQueue

}//end of: namespace



how can I get the worker thread running ?