473,382 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 5475
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.