Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, September 5, 2008 7:27 AM
I am working in C# desktop application
and I have two function
function 1()
{
lock()
{
.....
fun2();
}
}
fun2()
{
lock()
}
but getting error below
Object synchronization method was called from an unsynchronized block of code.
It's Me
All replies (3)
Friday, September 5, 2008 7:36 AM
That isn't your actual code - you would see errors about "Invalid expression term ')'" and ") expected" (plus "function 1" is not a valid method name) - you must lock something. Further, that error occurs if (for example) you try and Exit/Pulse/etc a lock that you don't own; the "lock" keyword by itself won't do this.
So what is your actual code?
[edit: locks are re-entrant, so there is no problem using lock on the same object in both "function 1" and "fun2", even if "function1" calls "fun2"]
Marc
Friday, September 5, 2008 7:40 AM
try
{
lock (_lock)
{
Socket client = (Socket)iar.AsyncState;
int recv = client.EndReceive(iar);
if (recv == 0)
{
conStatus.Text = "Client" + client.RemoteEndPoint.ToString() + " has disconnected.";
server.BeginAccept(new AsyncCallback(AcceptConn), server);
return;
}
else
{
DataRecive.Append(Encoding.ASCII.GetString(data, 0, recv));
string receivedData = Convert.ToString(DataRecive);
int IvalidateMessageNo = countCharacter(receivedData, "&"); // want to know how many time & occur in the retrive string .,,,,,,, working on socket programming
if (receivedData.EndsWith("listening skills"))
{
results.Items.Add(receivedData + client.RemoteEndPoint);
client.BeginReceive(data, 0, size, SocketFlags.None,
new AsyncCallback(ReceiveData), client);
}
client.BeginReceive(data, 0, size, SocketFlags.None,
new AsyncCallback(ReceiveData), client);
}
}
private static int countCharacter(string message, string Strsplitted)
{
lock (_lock)
{
string NewMessage = message;
NewMessage = NewMessage.Replace(Strsplitted, "");
return message.Length - NewMessage.Length;
}
}
It's Me
Friday, September 5, 2008 7:48 AM | 1 vote
First - why do you need a lock in countCharacter? It is side-effect free...
Second - where does the exception happen? On the blue lines?
Third - how is _lock defined? (it sometimes matters...)
Marc