Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. need help with adding a worker thread to my singleton

need help with adding a worker thread to my singleton

Scheduled Pinned Locked Moved C#
helpquestioncsharplinqdata-structures
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abiemann
    wrote on last edited by
    #1

    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 ?

    P L 2 Replies Last reply
    0
    • A abiemann

      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 ?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Should the thread be spun off in the constructor? And why have the constructor protected rather than private?

      A 1 Reply Last reply
      0
      • A abiemann

        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 ?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        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


        P 1 Reply Last reply
        0
        • L Luc Pattyn

          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


          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          Is that a good idea?

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            Is that a good idea?

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              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


              A Offline
              A Offline
              abiemann
              wrote on last edited by
              #6

              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.

              L P 2 Replies Last reply
              0
              • A abiemann

                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.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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


                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Should the thread be spun off in the constructor? And why have the constructor protected rather than private?

                  A Offline
                  A Offline
                  abiemann
                  wrote on last edited by
                  #8

                  "protected" is how Microsoft showed the singleton pattern on MSDN

                  P 1 Reply Last reply
                  0
                  • A abiemann

                    "protected" is how Microsoft showed the singleton pattern on MSDN

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    I bet they didn't seal it though.

                    1 Reply Last reply
                    0
                    • A abiemann

                      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.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #10

                      abiemann wrote:

                      a singleton was purely for good design

                      I agree with Luc. Singletons are rarely a good design.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups