need help with adding a worker thread to my singleton
-
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 ?
-
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 ?
Should the thread be spun off in the constructor? And why have the constructor protected rather than private?
-
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 ?
A static constructor/method/property cannot access any non-static members in its class; that includes both data members and code members (constructors/methods/properties). ==> make MessageWorker() a static method. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
A static constructor/method/property cannot access any non-static members in its class; that includes both data members and code members (constructors/methods/properties). ==> make MessageWorker() a static method. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Is that a good idea?
-
Is that a good idea?
PIEBALDconsult wrote:
Is that a good idea?
This[^] seems to suggest it isn't; and I wouldn't make the queue neither static nor singleton myself, however it was my attempt to answer the question in general, allowing the OP to get the code to compile. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
PIEBALDconsult wrote:
Is that a good idea?
This[^] seems to suggest it isn't; and I wouldn't make the queue neither static nor singleton myself, however it was my attempt to answer the question in general, allowing the OP to get the code to compile. :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
My original intent in creating the message queue as a singleton was purely for good design. Now I'll rework the class and simply ensure that the class isn't instantiated more than once.
Most often when I create a class, even when I only need one instance of it right away, sooner or later a bigger app will want to have more than one instance, so static and singleton aren't the right approach then. It is very seldom you can be sure you will never want more than one instance of anything. In your case, whatever you have now, you may end up wanting it all duplicated:another queue, another bunch of threads filling the queue, another backgroundworker emptying the queue. So why build in that restriction in the first place? :)
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
-
Should the thread be spun off in the constructor? And why have the constructor protected rather than private?
-
I bet they didn't seal it though.
-
My original intent in creating the message queue as a singleton was purely for good design. Now I'll rework the class and simply ensure that the class isn't instantiated more than once.
abiemann wrote:
a singleton was purely for good design
I agree with Luc. Singletons are rarely a good design.