473,320 Members | 1,810 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,320 software developers and data experts.

In case FileStream fails, the GC throws an exception in another thread that cannot be caught


Hi!
In the example below, once the media is full, the FileSteam.WriteByte throws
an exception and the code
is designed to handle it. However, when the GC is invoked, it calls the
Finalize of FileSteam, who is trying
to flush and close the stream, but the disk is full, so the flush fails and
another exception is thrown from another
(GC) thread. An ugly solution would be to use GC.SuppressFinalize(fs).
Is there a better solution or good practice? (closing withot flushing
perhaps)?

many thanks,
M. Rapp

--------------------------------

Example:

static void Main(string[] args)
{
try
{
FileStream fs =
new FileStream(args[0], FileMode.Create);
Console.WriteLine("starting to write...");
while (true)
fs.WriteByte(0xff);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}",
e.Message);
}
Console.WriteLine("press any key to continue");
Console.Read();
}
Nov 15 '05 #1
3 6278
Have you tried wrapping the use of your FileStreamer in a "using" block?
Something like:

using(FileStream fs =
new FileStream(args[0], FileMode.Create)) {
Console.WriteLine("starting to write...");
while (true)
fs.WriteByte(0xff);
}

You can always wrap the using block inside your try/catch block, but using
the implicit Dispose pattern you should be able to force the finalization to
occur in your thread (if I am not wrong).

using(
"Muki Rapp" <ra**@notalvision.com> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl...

Hi!
In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code
is designed to handle it. However, when the GC is invoked, it calls the
Finalize of FileSteam, who is trying
to flush and close the stream, but the disk is full, so the flush fails and another exception is thrown from another
(GC) thread. An ugly solution would be to use GC.SuppressFinalize(fs).
Is there a better solution or good practice? (closing withot flushing
perhaps)?

many thanks,
M. Rapp

--------------------------------

Example:

static void Main(string[] args)
{
try
{
FileStream fs =
new FileStream(args[0], FileMode.Create);
Console.WriteLine("starting to write...");
while (true)
fs.WriteByte(0xff);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}",
e.Message);
}
Console.WriteLine("press any key to continue");
Console.Read();
}

Nov 15 '05 #2
Hi!
I am afraid this is not the solution to the problem.
'using' will force the dispose method to be called and
exception to be thrown in the same thread, but still
at the end of the process the GC will also try to dispose
the filestream and an exception will be thrown in a DIFFERENT thread (run
example below for demonstration,
when filename path is a small floppy disk)

Any other ideas?

try
{
using(FileStream fs =
new FileStream(args[0], FileMode.Create))
{

while (true)
fs.WriteByte(0xff);

}
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}

"Stefano "WildHeart" Lanzavecchia" <st********@apl.it> wrote in message
news:u4*************@tk2msftngp13.phx.gbl...
Have you tried wrapping the use of your FileStreamer in a "using" block?
Something like:

using(FileStream fs =
new FileStream(args[0], FileMode.Create)) {
Console.WriteLine("starting to write...");
while (true)
fs.WriteByte(0xff);
}

You can always wrap the using block inside your try/catch block, but using
the implicit Dispose pattern you should be able to force the finalization to occur in your thread (if I am not wrong).

using(
"Muki Rapp" <ra**@notalvision.com> wrote in message
news:uI**************@TK2MSFTNGP11.phx.gbl...

Hi!
In the example below, once the media is full, the FileSteam.WriteByte

throws
an exception and the code
is designed to handle it. However, when the GC is invoked, it calls the
Finalize of FileSteam, who is trying
to flush and close the stream, but the disk is full, so the flush fails

and
another exception is thrown from another
(GC) thread. An ugly solution would be to use GC.SuppressFinalize(fs).
Is there a better solution or good practice? (closing withot flushing
perhaps)?

many thanks,
M. Rapp

--------------------------------

Example:

static void Main(string[] args)
{
try
{
FileStream fs =
new FileStream(args[0], FileMode.Create);
Console.WriteLine("starting to write...");
while (true)
fs.WriteByte(0xff);
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}",
e.Message);
}
Console.WriteLine("press any key to continue");
Console.Read();
}


Nov 15 '05 #3
> > > An ugly solution would be to use GC.SuppressFinalize(fs).
Is there a better solution or good practice? (closing withot flushing perhaps)?


I think you will have to use GC.SuppressFinalize(fs) after trying a
call to Dispose() or close() or using statement.
The FileStream's dispose (and IMO most "good" dispose methods) call
GC.SuppressFinalize at the end of the method call. However, because
of the exception, it is not getting called. So I wouldn't feel too
bad about manually supressing finalization.

I can't think of any other way to prevent an exception from being
raised on the Finalizer thread or catching that exception once thrown.

// a sample dispose function
void Dispose()
{
Dispose(true); // exception firing within this event...
GC.SuppressFinalize(this);
}

Although I do wonder if the file will continue to be locked since
Dispose was never called successfully. In the below program, it does
look like the file will be locked. Argh!

public static void Main ()
{
FileStream fs = new FileStream(@"A:\new.txt",
FileMode.Create);
bool diskfull = false;
try
{
while (true)
fs.WriteByte(0xff);
}
catch (IOException e)
{
Console.WriteLine("Exception: " + e.Message);
diskfull = true; // technically should only be set for
IOException when disk really is full.
}
finally
{
if (diskfull)
{
GC.SuppressFinalize(fs);
}
else
{
fs.Close();
}
}

// let's check to see if the file is locked.
try
{
using (FileStream fs1 = new FileStream(@"A:\new.txt",
FileMode.Open))
{
fs.ReadByte();
}
}
catch (Exception ex)
{
Console.WriteLine ("Exception: " + ex.Message);
}

Console.ReadLine();
}

Nov 15 '05 #4

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

Similar topics

0
by: John | last post by:
I've got multiple threads and processes that write to same file. Before writing all threads / processes first lock part of file and then write to file. If one thread / process locks file, another...
2
by: pegazik | last post by:
Hello. I have problem and I ask you for help. Probably there is some quite easy solution, but I can't see it. I'm trying to perform some action that have to be timeout safe. So here is the...
8
by: Robert Gravereaux | last post by:
I've noticed that the first exception thrown/caught by an app running in debug is very slow - it takes perhaps 7 or 8 seconds on my P4 machine. I've noticed this on several different machines...
5
by: David | last post by:
I am having a bit of a problem with catching an exception within a thread. Here is the scenario: I have a Windows Form. I create a new thread. This new thread calls a method in another DLL...
4
by: Anders Borum | last post by:
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...
4
by: Rob Richardson | last post by:
Greetings! I am working on an application that targets a Pocket PC running Windows CE and SQL Server CE. Almost all functions in the application use a Try block with a Catch block that looks...
0
by: upendra.desai | last post by:
Hi, I have written an application in Microsoft Visual C++ (Visual Studio 6.0) that uses MSXML 4.0 SP2. When I try to load an XML from a file, it throws an E_ACCESSDENIED (HRESULT = 0x80070005)...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
6
by: Emre DÝNÇER | last post by:
does c# provide an alternative to the throws keyword in javA?so that my client developer will strictly implement the exceptions that my methods may throw? thanks in advance
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.