CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    .Net Remoting Concurrency issue

    Using .Net remoting I create singleton serverside object that has some methods.
    When Clients call the methods, how can I enforce concurrency in the sense that each
    Method is called at a time and if other client call the same method, have it be put in the queue.
    It there any way to do it through static methods or thread safe way???

    Thank you

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: .Net Remoting Concurrency issue

    Add synchronization to the methods same as you would for implementing any thread safe class.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Re: .Net Remoting Concurrency issue

    could you elaborate on how to do it please

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: .Net Remoting Concurrency issue

    Look up the 'lock' keyword :

    Code:
    class MyExample
    {
        private int m_nValue = 0;
    
        public int Integer
        {
            get
            {
                lock (this)
                {
                    return m_nValue;
                }
            }
    
            set
            {
                lock (this)
                {
                    m_nValue = value;
                }
            }
        }
    }
    Should be self explanitory.

    There are other ways of doing synchronisation (i.e. making sure a piece of code is only entered once at a time) but this is probably the easiest in C#.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    Re: .Net Remoting Concurrency issue

    thank you for the code.

    by 'this' do you refer to the method??
    As far as I know lock lokst the varibales but not the methods itself, am I right??

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: .Net Remoting Concurrency issue

    Lock refers to the class instance.

    This of it this way :

    Whatever you pass into the lock is used as a variable to perform the lock.

    Another lock on this variable will cause it to block until the lock on the variable is released.

    So you can do this :

    Code:
    class MyClass 
    {
        private string m_sString = null;
    
        public MyClass()
        {
            m_sString = "Hello";
        }
    
        void GetString1()
        {
            lock (this)
            {
                return m_sString;
            }
        }
    
        void GetString2()
        {
            lock (m_sString)
            {
                return m_sString;
            }
        }
    }
    GetString1() allows GetString2() to be called by a seperate thread.

    Seperate threads will block on multiple calls to GetString1().

    Get the idea ? Locking a class doesn't lock all members.

    Don't forget to rate....

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured