472,985 Members | 2,571 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,985 software developers and data experts.

ReaderWriterLock with low timeout throws unhandled exception

Hello!

I am working on improving my threading skills and came across a question.
When working with the ReaderWriterLock class, I am getting an unhandled
exception if I acquire a WriterLock with a timeout less than the time
required to process the code section it protects.

The code listed below is just a small application I wrote to check the
behavious of the different threading / synchronization techniques. Basically
the application spins 10 threads with target method that acquires a
WriterLock (timout: 1000 ms) and simulates a long running operation (thread
sleeps for 500 ms).

A third thread that acquired the WriterLock times out, because the previous
two threads spent the available time with a long running process.

The logic answer I can think of is, that if one specifies a timeout, then
the CLR respects that and throws an exception regardless of what's inside
the code section. Handling the thrown exception is a matter of wrapping the
code section (including the acquisition of the WriterLock) in another
try/finally statement .. but that doesn't sound right.

A potential answer is to make sure that the timeout is sufficient for the
operations to complete (e.g. specify an infinite timeout).

...

Here's the error message from the unhandled exception:
An unhandled exception of type 'System.ApplicationException' occurred in
<application>. Additional information: This operation returned because the
timeout period expired.

Please look for "exception is thrown here" in the last method, to see where
I'm getting the exception.

class ThreadDriver
{
static ReaderWriterLock rwl = new ReaderWriterLock();

static void Main(string[] args)
{
Console.WriteLine("Spinning threads ..");

Thread[] threads = new Thread[10];

for (int i = 0; i < 10; i++)
{
Console.WriteLine("Spinning thread #{0}", i);

threads[i] = new Thread(new ThreadStart(Process));
threads[i].Name = "Thread ID: " + i;
threads[i].Start();
}

// Initiate join of spinning threads
for (int i = 0; i < 10; i++) { threads[i].Join(); }

// Calling thread is put on hold ..
Console.WriteLine("Press any key to continue ..");
Console.Read();
}

static void Process()
{
// Acquire lock with a short timeout
rwl.AcquireWriterLock(1000);
try
{
// Identify thread - exception is thrown here
Console.WriteLine(Thread.CurrentThread.Name);

// Simulates a long running process
Thread.Sleep(500);
}
catch (ApplicationException e)
{
Console.WriteLine(Thread.CurrentThread.Name + " failed " +
e.Message);
}
finally { rwl.ReleaseWriterLock(); }
}

I tried googling around looking for a direct answer but couldn't find one.
Not even the threading book from Wrox gives an answer to this (or I haven't
seen it :-).

Thanks in advance.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
4 5436
Anders Borum <an****@sphereworks.dk> wrote:
I am working on improving my threading skills and came across a question.
When working with the ReaderWriterLock class, I am getting an unhandled
exception if I acquire a WriterLock with a timeout less than the time
required to process the code section it protects.


Yes, just as documented in ReaderWriterLock.AcquireReaderLock and
AcquireWriterLock.

I'm not sure why this is a surprise...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Hello!

I must have been completely blind last night ..

From the docs: "If the time-out interval expires and the lock request has
not been granted, the method returns control to the calling thread by
throwing an ApplicationException. A thread can catch this exception and
determine what action to take next."

Looking at the sample code in the MSDN written for the ReaderWriterLock, I
didn't see that they're wrapping the AcquireWriterLock in another try/catch
statement. I think I was probably too tired to notice that .. and even
remember, that the AcquireWriterLock need to be in a try statement, if any
exception is to be caught.

static void Process()
{
try
{
// Acquire lock with a short timeout
rwl.AcquireWriterLock(1000);
try
{
// Identify thread - exception is thrown here
Console.WriteLine(Thread.CurrentThread.Name);

// Simulates a long running process
Thread.Sleep(500);
}
catch (ApplicationException e)
{
Console.WriteLine("timeout before acquisition");
}
finally { rwl.ReleaseWriterLock(); }
}
catch (ApplicationException e)
{
// do something with the exception here
}
}

Thanks in advance ..

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #3
Anders Borum <an****@sphereworks.dk> wrote:
I must have been completely blind last night ..

From the docs: "If the time-out interval expires and the lock request has
not been granted, the method returns control to the calling thread by
throwing an ApplicationException. A thread can catch this exception and
determine what action to take next."

Looking at the sample code in the MSDN written for the ReaderWriterLock, I
didn't see that they're wrapping the AcquireWriterLock in another try/catch
statement. I think I was probably too tired to notice that .. and even
remember, that the AcquireWriterLock need to be in a try statement, if any
exception is to be caught.


Well, there's no need for the AcquireWriterLock to be in a try
statement in that particular method - often the correct response will
be to let the exception bubble up the stack.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Hello!
Well, there's no need for the AcquireWriterLock to be in a try
statement in that particular method - often the correct response will
be to let the exception bubble up the stack.


That's true, but I was just writing a small app. to illustrate the
behaviour.

Thanks again, Jon!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Silvia | last post by:
I have a application web and when execute this for a long time generated this error: Server Error in '/RAIMServer' Application. -----------------------------------------------------------...
2
by: pokémon | last post by:
Question: Is this thread-safe: ReaderWriterLock rwl = new ReaderWriterLock(); Queue q = new Queue(); public int GetCount() { int val = 0; try {
7
by: Julie | last post by:
According to the documentation for the Acquire methods on the ReaderWriterLock class: -1 Infinite. 0 No time-out. > 0 The number of milliseconds to wait. ...
4
by: Paul | last post by:
I sometimes get a timeout error when populating my datagrid, the code is WizardConnection.Open() UpdateCommand.CommandText = "EXECUTE sp_assign_user '" & PhysOffice.SelectedValue & "', '" &...
5
by: Jason | last post by:
Hi all I get the following error when executing a rather intense stored procedure from an ASPX page. I have tried: - Increasing timeouts on IIS 5.0 (all areas that even mention timeout) - use...
4
by: VB Programmer | last post by:
When I run my ASP.NET 2.0 web app on my dev machine it works perfect. When I precomile it to my web deployment project and then copy the debug files to my web server I get this problem when trying...
1
by: Ron | last post by:
Hi, I had a stored procedure on SQL 2000 server to run calculation with large amount of data. When I called this stored procedure via System.Data.SqlClient.SqlCommand on production, i got error...
7
by: dadocsis | last post by:
I have a custom VB.net application that runs a query against an SQL server that takes more than 3 minutes. Right now after 3 minutes it times out. So far after doing my research I have changed the...
1
by: Scorpion657 | last post by:
Hey I really need help. I have a Website coded using ASP.NET and VB and for some reason, i'm getting the following error when I try to upload or access a large file which is stored in the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.