Object synchronization method was called from an unsynchronized block of code
-
Friday, September 05, 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- Changed Type jack 321 Tuesday, September 09, 2008 3:28 AM OP not follow
All Replies
-
Friday, September 05, 2008 7:36 AMThat 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- Edited by Marc GravellMVP Friday, September 05, 2008 7:38 AM reentrancy
-
Friday, September 05, 2008 7:40 AMtry
{
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 05, 2008 7:48 AMFirst - 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

