.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
Re: .Net Remoting Concurrency issue
Add synchronization to the methods same as you would for implementing any thread safe class.
Re: .Net Remoting Concurrency issue
could you elaborate on how to do it please
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.
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??
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.