Click to See Complete Forum and Search --> : .Net Remoting Concurrency issue
stardv
January 27th, 2005, 02:40 PM
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
TheCPUWizard
January 27th, 2005, 05:14 PM
Add synchronization to the methods same as you would for implementing any thread safe class.
stardv
January 27th, 2005, 05:22 PM
could you elaborate on how to do it please
darwen
January 27th, 2005, 06:41 PM
Look up the 'lock' keyword :
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.
stardv
February 3rd, 2005, 10:05 AM
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??
darwen
February 3rd, 2005, 10:39 AM
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 :
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.