Multithreading with C#
Subject:   Monitor.Enter not working
Date:   2005-08-25 10:34:08
From:   Mambr
Article discusses how one can have more elaborate locking mechanisms in C#, which I have tried before and they seem to fail.

I have a data server, which sends my app some messages and for every message I spawn a thread that tries to access some common code protected by Monitor.Enter(lockObject). This common code sends a reply to a server - server responds and that response releases object lock or calls Monitor.Exit(lockObject). Here is code:

private void sendCallbackMessageToServer(){
//do somethign and send message backif conditions are met if not then Monitor.Exit(lockObject)

//do not exit from Monitor here

//server sends messages thru this method
private void serverMessageEventHandler(String message){
//yada yada
if(message is the one I am looking for)
//do something and release lock

This does not work - it seems to work most of the time, but not always.
I placed increment counter right after Enter and decrement right before exit, which confirmed my suspition - whenever I send duplicate messages the counter is 2 (should be 1 as I start with 0 and write that info to disk before calling Exit).
any help would be greatly appreciated. email is


1 to 1 of 1
  1. Monitor.Enter not working
    2009-02-08 23:31:55  GhanshyamL [View]

1 to 1 of 1