473,394 Members | 1,709 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,394 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 6284
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:
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.