That was the problem.

I decided to write a test case for the alleged problem, and i couldn't
reproduce it. So after a more examination i discovered my bug.

Replaced lock operator with Try.Enter and forgot that lock is just a
Ergo didn't invoked Monitor.Exit

try {
        Enter(...)
} finally {
        Exit(...)
}

I didn't attributed the problem to my code, since the code run fine without
any concurrency,since the same thread was being used over and over and
TryEnter never blocked because the thread already had the lock. Oh well.

I guess I can either atribute the problem to lock operator is evil or I'm an
idiot who doesn't sleep much. :-D


Thanks.

--
Regard your good name as the richest jewel you can possibly be possessed of
- for credit is like fire;
when once you have kindled it you may easily preserve it, but if you once
extinguish it, you will find
it an arduous task to rekindle it again. The way to gain a good reputation
is to endeavor to be what you desire to appear."
                                                              [Socrates]

> -----Original Message-----
> From: Chad M. Gross [mailto:[EMAIL PROTECTED]
> Sent: quinta-feira, 17 de Julho de 2003 20:52
> To: [EMAIL PROTECTED]
> Subject: Re: [ADVANCED-DOTNET] Monitor.TryEnter timeout
> strange behaviour
>
>
> I have some test code below that illustrates how the TryEnter
> and Exit work together.  I have not seen or experienced the
> behavior you have explained.  It sounds almost like you are
> not calling Monitor.Exit from thread A and thread B is timing out.
>
> To use the code below, create a new console application
> project, copy the code into the default console application
> class file while replacing the existing class it generates.
> The way the code stands right now, thread 1 will block thread
> 2 since their ThreadStart methods both call TestLock() with
> the same object and Thread 2 will then do it's work once
> Thread 1 releases the lock.  If you want to see Thread 2 time
> out, increase the sleepTimeout to exceed the tryEnterTimeout.
>  If you want to see thread 1 and thread 2 run in parallel,
> have the Test2 method call TestLock with obj2.
>
>
> public class ThreadTest {
>
>  private const int tryEnterTimeout = 10000;
>  private const int sleepTimeout = 9000;
>  private object obj1 = 1;
>  private object obj2 = 1;
>
>  private void DisplayMessage(string test, string message)
>  {
>   Console.WriteLine(test + " - " + message + " - " +
> DateTime.Now.ToString());  }
>
>  private void TestLock(string test, object lockObject)
>  {
>   bool aquiredLock = false;
>   try
>   {
>    aquiredLock = Monitor.TryEnter(lockObject,
> tryEnterTimeout);
>    if (aquiredLock)
>    {
>     DisplayMessage(test, "Aquired Lock");
>     Thread.Sleep(sleepTimeout);
>     DisplayMessage(test, "After Sleep");
>    }
>    else
>    {
>     DisplayMessage(test, "Time Out");
>    }
>   }
>   finally
>   {
>    if (aquiredLock)
>    {
>     Monitor.Exit(lockObject);
>     DisplayMessage(test, "Released Lock");
>    }
>   }
>  }
>
>  public void Test1() {
>   TestLock("Test1", obj1);
>   DisplayMessage("Test1", "Complete");
>  }
>
>  public void Test2() {
>   TestLock("Test2", obj1);
>   DisplayMessage("Test2", "Complete");
>  }
> }
>
> class Class1
> {
>  /// <summary>
>  /// The main entry point for the application.
>  /// </summary>
>  [STAThread]
>  static void Main(string[] args)
>  {
>   ThreadTest threadTest = new ThreadTest();
>
>   Thread t1 = new Thread(new ThreadStart(threadTest.Test1));
>   Thread t2 = new Thread(new ThreadStart(threadTest.Test2));
>
>   t1.Start();
>   t2.Start();
>
>   Console.ReadLine();
>  }
> }
>
> Chad
>
>
> On Wed, 16 Jul 2003 18:38:46 +0100, TIAGO PASCOAL
> (Interaction) <[EMAIL PROTECTED]> wrote:
>
> > I'm using Monitor.TryEnter(object,int millisecondsTimeout)
> overload to
> wait
> >for a lock for no more than X amount of time. Now I'm
> getting a strange
> >behaviour.
> >
> >From what I understand from the specification using this overload I
> >would get the same behaviour as using the lock keyword (C#)
> but on wait
> >no more than millisecondsTimeout.
> >
> >What I'm getting is the lock is ONLY obtained after
> millisecondsTimeout
> >elapses EVEN when the lock has been released.
> >
> >Going to try to make graphic representation of a timeline
> >                                   x Seconds
>  120 seconds
> >(after)
> >-------+--------+-------+--------+-------+--------+-------+--------+
> >
> >Thread A  TryEnter(obj,120000)   LOCK_RELEASED
> >Thread B     TryEnter(obj,120000);
>  Thread B
> >unblocks
> >
> >Thread A gets hold of the lock first and Thread B blocks.
> >
> >Thread B should unblock on x Seconds and only unblocks after 120
> >seconds.
> >
> >If I replace TryEnter with lock(obj) thread B unblocks right after
> >thread
> A
> >has
> >Released the lock.
> >
> >Any thoughts?
> >
> >TIA
> >
> >
> >
> >
> >--
> >Regard your good name as the richest jewel you can possibly be
> >possessed
> of
> >- for credit is like fire;
> >when once you have kindled it you may easily preserve it, but if you
> >once extinguish it, you will find it an arduous task to rekindle it
> >again. The way to gain a good reputation is to endeavor to
> be what you
> >desire to appear."
> >
> >[Socrates]
> >
> >
> >*************************************************************
> **********
> >***
> **
> >
> >
> >Esta mensagem (incluindo eventuais ficheiros anexos) pode conter
> informacao
> >confidencial ou privilegiada.
> >Se nao for o destinatario pretendido, por favor contacte
> imediatamente
> >o remetente por e-mail, e apague a mensagem do seu sistema
> informatico.
> >A transmissao de mensagens por e-mail nao e absolutamente segura ou
> >livre
> de
> >erro: a mensagem pode ser interceptada, alterada, perdida, destruida,
> chegar
> >ao seu destinatario num momento posterior ao pretendido ou
> alterada, ou
> >ainda com virus. O Banco Espirito Santo declina qualquer
> >responsabilidade por erros ou omissoes na presente mensagem que
> >resultem das circunstancias descritas. Qualquer opiniao expressa na
> >presente mensagem e imputavel a pessoa que a enviou, a nao ser que o
> >contrario resulte expressamente do seu texto e
> que a
> >pessoa em causa tenha poderes para vincular o Banco Espirito Santo.
> >
> >This message and any files transmitted with it may contain
> confidential
> >information or privileged material. If you are not the intended
> >recipient, please notify the sender
> immediately
> >by e-mail and delete this message from your system.
> >E-mail transmission cannot be guaranteed to be secure or
> error-free as
> >information could be intercepted, corrupted, lost, destroyed, arrive
> >late
> or
> >incomplete, or contain viruses. Banco Espirito Santo
> therefore does not
> >accept liability for any errors or omissions in the contents of this
> message
> >which arise as a result of e-mail transmission.
> >Any views expressed in this message are those of the
> individual sender,
> >except where the message states otherwise and the sender is
> authorized
> >to state them to be the views of Banco Espirito Santo.
> >
> >*************************************************************
> **********
> >***
> **
>


****************************************************************************


Esta mensagem (incluindo eventuais ficheiros anexos) pode conter informacao
confidencial ou privilegiada.
Se nao for o destinatario pretendido, por favor contacte imediatamente o
remetente por e-mail, e apague a mensagem do seu sistema informatico.
A transmissao de mensagens por e-mail nao e absolutamente segura ou livre de
erro: a mensagem pode ser interceptada, alterada, perdida, destruida, chegar
ao seu destinatario num momento posterior ao pretendido ou alterada, ou
ainda com virus. O Banco Espirito Santo declina qualquer responsabilidade
por erros ou omissoes na presente mensagem que resultem das circunstancias
descritas.
Qualquer opiniao expressa na presente mensagem e imputavel a pessoa que a
enviou, a nao ser que o contrario resulte expressamente do seu texto e que a
pessoa em causa tenha poderes para vincular o Banco Espirito Santo.

This message and any files transmitted with it may contain confidential
information or privileged material.
If you are not the intended recipient, please notify the sender immediately
by e-mail and delete this message from your system.
E-mail transmission cannot be guaranteed to be secure or error-free as
information could be intercepted, corrupted, lost, destroyed, arrive late or
incomplete, or contain viruses. Banco Espirito Santo therefore does not
accept liability for any errors or omissions in the contents of this message
which arise as a result of e-mail transmission.
Any views expressed in this message are those of the individual sender,
except where the message states otherwise and the sender is authorized to
state them to be the views of Banco Espirito Santo.

****************************************************************************

Reply via email to